Creating a Phonebook program using classes

Hello everyone, second semester CS student here need some help with a programming assignment that I have. So our previous project was to create a phonebook program (this: https://gist.github.com/anonymous/94aee437b828856eb329 )

Now our next assignment is to create a class to perform all the operations that the functions were previously doing. Now I'm having trouble wrapping my head around a struct nested within the class and how to iterate through the contacts in the function.

Here are the objectives for the assignment:

-Use the same Contact struct that you defined in Lab 5. The Phonebook should store the list of Contacts as a private data member (you may use a vector as in Lab 5).

-Phonebook should implement the following methods, using the exact return and input types as shown:

void addContact(const Contact &cont)

Contact findContact(string firstName, string lastName)
If no match is found, have this return a Contact with an empty string for the first/last name.

void saveContacts(string fileName)

-All cin/cout statements should remain outside the class. Do not put any cin/cout statements inside the class.
The program's overall behavior and output should be the same as the previous lab.

This is what I've come up with so far:
https://gist.github.com/anonymous/cd3ddb53ca69bd3036ba

Adding contacts seems to work fine but when I try to search for a contact the values within the struct seem to be resetting.

Any and all help/code corrections/tips are greatly appreciated guys thanks a bunch!

edit: Also, sorry for any stray statements I was in the midst of debugging when i posted this question :P
Last edited on
Posting your code on the forums would make people more likely to help you, but I can understand not doing so when your code gets to be a couple hundred lines.

1. Your class structure doesn't conform with the assignment.

14
15
16
17
18
19
20
21
struct Contact
{
	string _firstName[MAX_NUM_CONTACTS];
	string _lastName[MAX_NUM_CONTACTS];
	string _phoneNumber[MAX_NUM_CONTACTS];
	string _emailAddress[MAX_NUM_CONTACTS];
	int _numContacts;
};

Your class is supposed to contain an array (or vector, or whatever container you want to use) of these. Contact itself is not supposed to be a contain a bunch of arrays.

2. Line 22, Contact Contact;. You named your Contact variable the same as its type. I guess it works, but it's bad practice.

3. Your add function doesn't conform to the programming assignment.

35
36
37
38
39
40
void addContact(const Phonebook &Conatcts)
{
	cin >> Contact._firstName[Contact._numContacts] >> Contact._lastName[Contact._numContacts];
	cin >> Contact._phoneNumber[Contact._numContacts] >> Contact._emailAddress[Contact._numContacts];
	Contact._numContacts++;
}

A problem here is that you define the parameter as being of the Phonebook class when your assignment clearly states it should be of type Contact. You are supposed to actually add the provided Contact to your array, not ask for input and create the Contact, then add it. This is made more problematic because of #1 (you aren't using an array of Contact).

4. These methods are supposed to be part of your class. You should be using the class's methods in your code.

68
69
70
void addContact(Phonebook Contact, int numContacts);
void showContact(Phonebook Contact, int numContacts);
void saveContact(Phonebook Contact, int numContacts);


5.
Adding contacts seems to work fine but when I try to search for a contact the values within the struct seem to be resetting.


That's because of the way you implemented those non-member functions in #4 and the way you're using them. It would sort of work if you passed the Phonebook parameter by reference, but you're passing by value.

That means anything you do to it inside the scope of those functions isn't saved to the actual Phonebook inside main.

Haven't finished going over the code, but that answers your original question and gives you enough to think about.
Topic archived. No new replies allowed.