#include using namespace std; /* ContactList.cpp * purp: maintain a contact list, as used in a cellphone * does: adds entries, lists entries, searches, partial matches * todo: split into functions * todo: lookup by number (used to display name of incoming call) * more: add other attributes */ const int MAX_ITEMS = 10000; const int NOT_FOUND = -1; void menu(string[], string[]); void show_choices(); int lookup(string[], string[], int); void print(string[], string[], int); void modify(string[], string[], int); int add(string[], string[], int); int del(string[], string[], int); int find(string[], string[], int, string); int main() { // define arrays here for storage string nm[MAX_ITEMS]; string pn[MAX_ITEMS]; menu(nm, pn); return 0; } // run the main menu -- show choices, get selection, do action, repeat void menu(string nm[], string pn[]) { int n; // keeps count of number of entries in list char choice; // user menu choice n = 0; do { show_choices(); cout << "\nChoice: "; cin >> choice; switch(choice){ case 'p': print(nm, pn, n); break; case 'a': n = add(nm, pn, n); break; case 'd': n = del(nm, pn, n); break; case 'l': lookup(nm, pn, n); break; case 'm': modify(nm, pn, n); break; case 'c': break; case 'x': break; default: cout << "Unknown command\n"; } cout << endl; } while( choice != 'x' ); } // // display list of choices // void show_choices() { cout << "\nContactList 1.0\n\n"; cout << " p: print list\n"; cout << " a: add entry\n"; cout << " d: delete entry\n"; cout << " l: lookup person\n"; cout << " c: clear all entries\n"; cout << " m: modify entry\n"; cout << " x: exit\n"; } // print the list, one line per entry void print(string nm[MAX_ITEMS], string pn[MAX_ITEMS], int n) { int i; for(i=0; i> name; cout << "phone number? "; cin >> num; nm[n] = name; pn[n] = num; n++; return n; } // // del an entry, return the new number of items // int del(string nm[MAX_ITEMS], string pn[MAX_ITEMS], int n) { int who_to_rem; int i; string ans; who_to_rem = lookup(nm, pn, n); if ( who_to_rem == NOT_FOUND ){ return n; } cout << "Are you SURE (y/n)? "; cin >> ans; if ( ans != "y" ){ return n; } n--; for(i=who_to_rem; i> newnum; pn[item_to_change] = newnum; } } // // lookup person by name // int lookup(string nm[MAX_ITEMS], string pn[MAX_ITEMS], int n) { int where_it_is; string name; cout << "Name? "; cin >> name; where_it_is = find(nm, pn, n, name); // find it if ( where_it_is == NOT_FOUND ){ cout << "Cannot find " << name << endl; } else { cout << nm[where_it_is] << " " << pn[where_it_is] << endl; } return where_it_is; }