home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1001.CPP
- //--------------------------------------------------------------
- // main program to exercise PhoneBk classs
- //--------------------------------------------------------------
-
- // files in this example:
- // %F,15,EX10011.H%EX10011.H definition of the class String
- // %F,15,EX10012.H%EX10012.H definition of the classes
- // PhoneNum, List and ListIter
- // %F,15,EX10013.H%EX10013.H definition of the class PhoneBk
- // %F,15,EX10011.CPP%EX10011.CPP member functions of the class String
- // %F,15,EX10012.CPP%EX10012.CPP member functions of the classes
- // PhoneNum, List, and Listiter
- // %F,15,EX10013.CPP%EX10013.CPP member functions of the class PhoneBk
- // EX1001.CPP this file -- main() to exercise this class
-
- //--------------------------------------------------------------
- #include "EX10013.H" // definition of PhoneBk class
- #include "EX10012.H" // definition of PhoneNum, and List
-
- void userinfo(); // display operating instructions
-
- // This is a small interactive application.
- // Create an empty phonebook object and perform user commands.
- // USER COMMANDS: i name number insert an entry
- // r name remove an entry
- // l name lookup name and display number
- // p print contents of phonebook
- // q quit
- //
- // Simplifying assumptions: no input verification
- // all names are one word
- // all names are unique
- // all phone numbers are all digits
-
- //--------------------------------------------------------------
- main()
- { PhoneBk TelBook; // TelBook is a PhoneBk object
- char person[20]; // user input - a name
- long telephone; // user input - a telephone number
- char command; // user input - a command ( irldq )
- userinfo(); // display operating instructions
- do // begin command interpreter
- {
- cout << "READY> "; // print prompt
- cin >> command; // read command
- switch (command) // select action for command
- { case 'i': // insert command
- cin >> person // read a name
- >> telephone; // read a phone number
- // recover from non-numeric input
- if ( ! cin) // test for cin error
- { cin.clear(); // reset cin error state
- char junk[20]; // skip offending input
- cin >> junk;
- cout << junk << '?' << endl;
- break;
- }
- // try to insert an entry into phonebook
- if ( ! TelBook.insert( person, telephone) )
- // if insertion failed, print message
- cout << "Cannot make an entry for "
- << person
- << endl;
- break;
- case 'l': // lookup command
- cin >> person; // read a name
- // try to look up phone number for name
- telephone = TelBook.lookup( person);
- // if the number is found, print it
- if ( telephone )
- cout << TelBook.lookup (person)
- << endl;
- // if the number is not found, print message
- else
- cout << "Cannot find "
- << person
- << endl;
- break;
- case 'r': // remove command
- cin >> person; // read a name
- // try to remove entry from phonebook
- if ( ! TelBook.remove( person) )
- // if removal failed, print message
- cout << "No entry for "
- << person
- << endl;
- break;
- case 'p': // print command
- TelBook.print( cout) << endl;
- break;
- case 'q': // quit command
- break;
- case '?': // repeat instructions
- userinfo();
- break;
- default : // command not recognized
- cout << command
- << "? Type ? for help"
- << endl;
- break;
- }; // repeat until command is quit
- } while (command != 'q');
- return ( 0 ); // end of program
- }
-
- // function to display operating instructions for user
-
- void userinfo()
- { cout << " TEST PROGRAM FOR PHONE BOOK CLASS" << endl;
- cout << "For simplicity commands are 1 letter, "
- << "names are one word, phone"
- << endl;
- cout << "numbers are digits (no '-'), "
- << "and there is no error checking."
- << endl;
- cout << " At the READY> prompt, enter: "
- << endl;
- cout << "To INSERT or CHANGE a number: "
- << "READY> i <name> <number>"
- << endl;
- cout << "To LOOKUP a number : "
- << "READY> l <name>"
- << endl;
- cout << "To REMOVE a number : "
- << "READY> r <name>"
- << endl;
- cout << "To Print : "
- << "READY> p"
- << endl;
- cout << "To QUIT : "
- << "READY> q"
- << endl;
- cout << "To repeat these instructions: "
- << "READY> ?"
- << endl
- << endl;
- }
-