I'm in an intro C++ class and am suppose to make a phonebook program that will show a list of commands then read in a name,number ,and notes into a file. Then be able to retrieve a name, or display the whole contact list. I'm having trouble getting the find option and list option to work, any help would be great. This is the code I have so far.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct contact{
    string name, number, notes;
};
ofstream output("c:\\phonedata\\contactlist.txt");

int rec_num = 0;
int num_entries = 0;

string toupper (string S){
    for(size_t i=0; i < S.length(); i++)
        S[i] = toupper(S[i]);
    return S;
}
void choice(){
    cout << "The avaliable options are\n";
    cout << "Enter e to enter a new name. \n";
    cout << "Enter f to find a name. \n";
    cout << "Enter l to see your contact list. \n";
    cout << "Enter q to quit the program \n";
    cout << "\n";
}
class Entry{
public:
    string name, number, notes;
    contact contactlist[100];
};
void listAllcontacts(){
    cout << "Name: " << contactlist.name;
}
string notes[40];
int main(){
    char command;
    char notes[40];
    string name, number;
    string Filename;

    choice();
    cout << "Command: ";
    cin >> command;
    while (command != 'q'){
        switch(command){
            case 'e': cin >> name; cout <<"enter number\n";
                      cin >> number; cout <<"enter notes\n";
                      cin.getline(notes,40); break;
            case 'f': cin >> name; find ;name(name); break;
            case 'l': listAllcontacts(); break;
        }
    }
    if (command == 'q')
        cout << "Good bye";
    return 0;
}

For case 'e', how are you adding the new info into your list? For case 'l', how do you plan to iterate over your list and display each contact? And obviously case 'f' needs work.

Since I don't know specifically where you're stuck, I suspect the problem is that you're trying to brute-force your way through the assignment by thinking strictly in terms of the code. Your overall structure in main() is mostly fine, so you have thought through the highest level. Now just break down each step into what it needs to do. For example, adding a new contact includes prompting the user, accepting input (both of which you've done), creating a new contact object using that input, and adding the contact object into the list. Break each step into further sub-steps before you worry about writing the code.

You have a contact struct, so you don't also need an Entry class. Instead you need a class to manage your list of contacts, maybe you want to call it ContactList. What members does it need? What methods? (Hint: it doesn't need a name, number and notes fields.)

Once you have the code working, there are other optimizations you can make. For example, you could include getting the desired command from the user into your choice() function: char choice() { ...; return command; }. Or note that the only way to get to the line numbered 13 above, is if command is already 'q', so you don't need to check for it again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.