C++ Phonebook Program, 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;
}
Topic archived. No new replies allowed.