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).
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;
}
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.
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.