C++ Phonebook project, Need Help

I am given the code below and need to add a few things to it: after developing the code i have these are still the things I really am not sure how to finish. Any help would be greatly appreciated. Thank you

- Add a command ‘d’ for delete to delete an entry corresponding to a name typed by the user.

- Add an edit command to allow a user to edit one or more of the fields in the phonebook

- Allow a partial match to find or delete an entry. For example, “F mi” would match any entry with “mi” in the name, for example “Smith” or “Hermit” or Mitchell. You may use the function “find” in C++ for this feature.

- Make the program object-oriented. This means having a separate class for the internal representation of a phonebook, with methods like sort, list, or delete.

- The phonebook class must be defined with a separate header file (.h) and a separate implementation file (.cpp). The header file will include public and private variables and function prototypes.

- The implementation (.cpp) file for the phonebook class will include the implementation of all functions in the class. The definition of these functions must use the scope resolution operator to define methods (i.e. no inline functions within a class).






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

struct Contact {
string name, number, notes;
};
Contact contactList[100];

int rec_num = 0;
int num_entries;

string toUpper (string S) {
for (int i= 0; i<S.length(); i++)
S[i] = toupper(S[i]);
return S;
}

void ReadFile () {
string S;
fstream input("PhoneData.txt");
while (!input.eof() && !input.fail()){
input >> contactList[rec_num].name
>> contactList[rec_num].number;
getline(input, S);
contactList[rec_num].notes = S;
rec_num++;
}
cout << "Book read." << endl;
num_entries = rec_num;
input.close();
return;
}
void StoreFile () {
fstream F("PhoneData.txt");
rec_num = 0;

while (rec_num < num_entries){
F << contactList[rec_num].name << " "
<< contactList[rec_num].number << " "
<< contactList[rec_num].notes << " " << endl;
rec_num++;
}
cout << "Phonebook stored." << endl;
return;
}

void add_name(string name, string number, string notes){
contactList[num_entries].name = name;
contactList[num_entries].number = number;
contactList[num_entries].notes = notes;
num_entries++;
return;
}
void retrieve_name(string name){
for (int i = 0; i < num_entries; i++){
if (toUpper(contactList[i].name) == toUpper(name)) {
cout << "Phone Number: " << contactList[i].number << endl
<< "Notes: " << contactList[i].notes << endl;
return;
}
}
cout << "Name not found" << endl;
return;
}

void sortList() {
int i;
Contact temp;

for(int j=0; j < num_entries; j++) {
for(i = j+1; i < num_entries; i++) {
if(contactList[j].name.compare(contactList[i].name) > 0) {
//Swap the entries if their names are not in ascending order
temp = contactList[j];
contactList[j] = contactList[i];
contactList[i] = temp;
}
}
}
cout << "Sorted the list. Press l to list all the contacts";
}

void listAllContacts() {
int i = 0;
while (i < num_entries) {
cout << "-- " << contactList[i].name << " "
<< contactList[i].number << endl
<< "-- " << contactList[i].notes << endl << endl;
i++;
}
}

int main(){
string name, number, notes;
string FileName;
char command;

FileName = "PhoneData.txt";
ReadFile ();
cout << "Use \"e\" for enter, \"f\" for find, \"q\" to quit."
<< endl << "Command: ";
cin >> command;
while (command != 'q'){
switch (command){
case 'e': cin >> name; cout << "Enter Number: ";
cin >> number; cout << "Enter Notes: ";
cin.ignore(); getline(cin, notes);
add_name(name, number, notes); break;
case 'f': cin >> name; retrieve_name(name); break;
case 's': sortList(); break;
case 'l': listAllContacts(); break;
}
cout << "\nCommand: "; cin >> command;
}
StoreFile();
cout << "All set !";
return 0;
}
Last edited on
First things first, you need to change your generic "Contacts" array into a standard container, I suggest a vector: http://www.cplusplus.com/reference/stl/vector/
Would that be necessary to do? I haven't learned to do that yet.
It is not necessary, but would help you 100 times over.

A few reasons why: It takes care of things that you don't have to, such as adding new elements if you need to. In your program contacts holds 100 elements? What if the user wants more? What if he wants less? If you use a vector, you don't have to worry about how many elements the user wants, cus you add or delete them during runtime without any problem.

Next reason I would choose is functionality. Lot of nice things you can do with vectors. And I think it checks for out of bounds errors too.
Ok I see that now, but how would I go about replacing my contacts array with a vector?
First thing you would do is go to the top of this page.
Look for the word and punctuation "Search:" and then click into the box next to it.
Now, type "vector". Press enter. Click the first link "vector - C++ Reference" and read.

Good luck!
Last edited on
Here is an example of using a vector; it is much like an array but it manages its size for you:
1
2
3
4
5
6
7
8
9
10
11
12
std::vector<int> ints;

ints.push_back(3);
ints.push_back(7);
ints.push_back(2);
ints.push_back(99);
ints.push_back(-4);

for(unsigned int i = 0; i < ints.size(); ++i)
{
    std::cout << ints[i] << std::endl;
}
Last edited on
Topic archived. No new replies allowed.