Question

This program is used to create a phonebook for storing contact information into a text file....

This program is used to create a phonebook for storing contact information into a text file.

The program begins by asking the user to provide a name for the file that will contain their phone book information. It will then ask the user to provide the names and numbers of their contacts. It should keep asking the user for contact information until they type in Done (case-sensitive).

After the user types Done, store all contact information into the file name they provided at the beginning of the program. If the user does not add any contacts, then the file should be empty.

Please see the sample output below to guide the design of your program.

Sample Output:

Please provide the file name for your phone book: contacts.txt

Contact 1:
Please provide the name: Elsie Simon
Please provide their number: (202)555-0115

Contact 2:
Please provide the name: Kaisha Cope
Please provide their number: 5455689016

Contact 3:
Please provide the name: Sultan Vargas
Please provide their number: 371-998-4166

Contact 4:
Please provide the name: Done

Saving phonebook into contacts.txt

Done!

in c++

Q2) This program loads contact information stored in a text file and displays it on the screen.

The program asks the user to provide the filename of a phonebook file. It will then load all of the names and numbers stored in the file and display them on the screen.

We assume that the file is stored using the correct format wherein for each contact, there is always a name followed by their phone number. Below is an example of a possible phonebook file.

Sample phonebook file (phonebook.txt):

Shanay Wickens
7205972770
Harlee Collins
3328206140
Addison Ryan
7917471622

Please see the sample output below assuming it loaded the phonebook file shown above to guide the design of your program. In case there are no contacts, inform the user that there were no contacts stored in the file. If the file does not exist, inform the user that the phonebook file is missing.

Sample Output:

Please provide the file name for your phone book: phonebook.txt

Contact 1:
Name: Shanay Wickens
Number: 7205972770

Contact 2:
Name: Harlee Collins
Number: 3328206140

Contact 3:
Name: Addison Ryan
Number: 7917471622

in c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,Please find the solution and rate the answer. Q2 please post seperately.

#include <iostream>
#include "string"
#include "fstream"

class Contact {
    char name[100];
    char phone[50];
public:
    const char* toString() {
        std::string s;
        s.append("");
        s.append("Name:");
        s.append(name);
        s.append("Phone");
        s.append(phone);
        s.append("\n");
        return s.c_str();
    }

    const char *getName() const {
        return name;
    }

    const char *getPhone() const {
        return phone;
    }

    void setName(char *item) {
        strcpy(Contact::name, item);
    }

    void setPhone(char *phone) {
        strcpy(Contact::phone, phone);
    }

    Contact *next;
};

Contact *head;

void add(Contact *newN) {
    newN->next = nullptr;
    if (head == nullptr) {
        head = newN;
        return;
    }
    Contact *temp = head;
    while (temp->next != nullptr) {
        temp = temp->next;
    }
    temp->next = newN;
}

char *removeEndNewLine(const char *temp) {
    int length = strlen(temp);
    char *newCh = new char[length - 1];
    strcpy(newCh, temp);
    newCh[length - 1] = '\0';
    return newCh;

}

int main() {
    char data[100];
    std::cout << "Please provide the file name for your phone book: ";
    fgets(data, 100, stdin);
    bool loop = true;
    int i = 1;
    do {
        std::cout << "Contact " << i++;
        char name[100], number[20];
        std::cout << "Please provide the name:";
        fgets(name, 100, stdin);

        if (strcmp(removeEndNewLine(name), "Done") == 0) {
            break;
        }
        Contact *temp = new Contact();
        temp->setName(removeEndNewLine(name));

        std::cout << "Please provide their number:";
        fgets(number, 20, stdin);
        if(strcmp("\n",number)==0){
            fgets(number, 20, stdin);
        }
        temp->setPhone(removeEndNewLine(number));
        add(temp);
    } while (loop);
    Contact *temp = head;

    std::ofstream str;
    str.open(removeEndNewLine(data));
    while (temp != NULL) {
        str << removeEndNewLine(temp->toString());
        str<<"\n";
        temp = temp->next;
    }
    str.close();
    return 0;
}

Sample out: See the file has stored the output.

Add a comment
Know the answer?
Add Answer to:
This program is used to create a phonebook for storing contact information into a text file....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • In this assignment, you will create an application that holds a list of contact information. You ...

    In this assignment, you will create an application that holds a list of contact information. You will be able to prompt the user for a new contact, which is added to the list. You can print the contact list at any time. You will also be able to save the contact list. MUST BE IN PYTHON FORMAT Create the folllowing objects: ContactsItem - attributes hold the values for the contact, including FirstName - 20 characters LastName - 20 characters Address...

  • In this assignment, you will create an application that holds a list of contact information. You...

    In this assignment, you will create an application that holds a list of contact information. You will be able to prompt the user for a new contact, which is added to the list. You can print the contact list at any time. You will also be able to save the contact list. Must Be In Python Format Create the following objects: ContactsItem - attributes hold the values for the contact, including FirstName - 20 characters LastName - 20 characters Address...

  • Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static...

    Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...

  • language:python VELYIEW Design a program that you can use to keep information on your family or...

    language:python VELYIEW Design a program that you can use to keep information on your family or friends. You may view the video demonstration of the program located in this module to see how the program should function. Instructions Your program should have the following classes: Base Class - Contact (First Name, Last Name, and Phone Number) Sub Class - Family (First Name, Last Name, Phone Number, and Relationship le. cousin, uncle, aunt, etc.) Sub Class - Friends (First Name, Last...

  • Problem: Contact Information Sometimes we need a program that have all the contact information like name,...

    Problem: Contact Information Sometimes we need a program that have all the contact information like name, last name, telephone, direction and something we need to know about that person (notes). Write a program in java language with GUI, classes, arrays, files and inheritance that make this possible. In the first layer, the user should see all the contacts and three buttons (add, delete, edit). *Add button: If the user click the button add, a new layer should appear and should...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • Exercise 3) Creating a QT program to search for Customer Phone Number : Create a text...

    Exercise 3) Creating a QT program to search for Customer Phone Number : Create a text file (customer.txt) and put names and phone numbers into it. each name has to have its own phone number right under it. In the QT window, the user has to enter a name and the program prints the phone number related to it. NB: If the user input a name that doesn't exist, the program should print an error message. Text file example: QT...

  • Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names...

    Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names and phone numbers. ·         a. Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables. b.Add the following operations to your program: i. Add a new contact. Ask the user to enter the name and up to 3 phone numbers. ii. Delete a contact...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT