home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX06022.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  5.5 KB  |  137 lines

  1. // \EXAMPLES\EX06022.CPP
  2. // the prototype implementation of a phonebook class
  3. //  main program to exercise PhoneBk classs
  4.  
  5. //--------------------------------------------------------------
  6. //  files in this example:
  7. // %F,15,EX0602.H%EX0602.H      definition of the class PhoneBk
  8. // %F,15,EX06021.CPP%EX06021.CPP   member functions of the class PhoneBk
  9. // EX06022.CPP   this file -- main() to exercise this class
  10.  
  11. //--------------------------------------------------------------
  12. #include "EX0602.H"
  13.  
  14. //--------------------------------------------------------------
  15. void userinfo();              // display operating instructions
  16.  
  17. //--------------------------------------------------------------
  18. // This is a small interactive application.
  19. // Create an empty phonebook object and perform user commands.
  20. // USER COMMANDS: i name number              insert an entry
  21. //                r name                     remove an entry
  22. //                l name           lookup name and display number
  23. //                d                duplicate & display phonebook
  24. //                q                          quit
  25. //
  26. // Simplifying assumptions:  no input verification
  27. //                           all names are one word
  28. //                           all names are unique
  29. //                           all phone numbers are all digits
  30.  
  31. //--------------------------------------------------------------
  32. main()
  33. { PhoneBk TelBook;        // TelBook is a PhoneBk object
  34.   char person[20];        //  user input - a name
  35.   long telephone;         //  user input - a telephone number
  36.   char command;           //  user input - a command ( irldq )
  37.   userinfo();             //  display operating instructions
  38.   do                  //  begin command interpreter
  39.   {
  40.     cout << "READY> ";        //  print prompt
  41.     cin >> command;           //  read command
  42.     switch (command)          //  select action for command
  43.     {  case 'i':                  //  insert command
  44.                    cin >> person      //  read a name
  45.                        >> telephone;  //  read a phone number
  46.                      // recover from non-numeric input
  47.                    if ( ! cin)        // test for cin error
  48.                    { cin.clear();     // reset cin error state
  49.                      char junk[20];   // skip offending input
  50.                      cin >> junk;
  51.                      cout << junk << '?' << endl;
  52.                      break;
  53.                    }
  54.                      // try to insert an entry into phonebook
  55.                   if ( ! TelBook.insert( person, telephone) )
  56.                      // if insertion failed, print message
  57.                     cout << "Cannot make an entry for "
  58.                          << person
  59.                          << endl;
  60.                   break;
  61.        case 'l':                  //  lookup command
  62.                   cin >> person;      //  read a name
  63.                   //  try to look up phone number for name
  64.                   telephone = TelBook.lookup( person);
  65.                   // if the number is found, print it
  66.                   if ( telephone )
  67.                     cout << TelBook.lookup (person)
  68.                          << endl;
  69.                   // if the number is not found, print message
  70.                   else
  71.                     cout << "Cannot find "
  72.                          << person
  73.                          << endl;
  74.                   break;
  75.        case 'r':                 //  remove command
  76.                   cin >> person;     // read a name
  77.                   // try to remove entry from phonebook
  78.                   if ( ! TelBook.remove( person) )
  79.                   // if removal failed, print message
  80.                     cout << "No entry for "
  81.                          << person
  82.                          << endl;
  83.                   break;
  84.        case 'p':                 // print command
  85.                   TelBook.print( cout) << endl;
  86.                   break;
  87.        case 'q':               //  quit command
  88.                   break;
  89.        case '?':               //  repeat instructions
  90.                   userinfo();
  91.                   break;
  92.        default :               //  command not recognized
  93.                   cout <<  command
  94.                        << "?  Type ? for help"
  95.                        << endl;
  96.                   break;
  97.      };                   //  repeat until command is quit
  98.   } while (command != 'q');
  99.   return ( 0 );           //  end of program
  100. }
  101.  
  102. //--------------------------------------------------------------
  103. //  function to display operating instructions for user
  104.  
  105. void userinfo()
  106. { cout << "            TEST PROGRAM FOR PHONE BOOK CLASS"
  107.        << endl;
  108.   cout << "For simplicity commands are 1 letter, "
  109.        << "names are one word, phone"
  110.        << endl;
  111.   cout << "numbers are digits (no '-'), "
  112.        << "and there is no error checking."
  113.        << endl;
  114.   cout << " At the READY> prompt, enter: "
  115.        << endl;
  116.   cout << "To INSERT or CHANGE a number:  "
  117.        << "READY>  i  <name> <number>"
  118.        << endl;
  119.   cout << "To LOOKUP a number          :  "
  120.        << "READY>  l  <name>"
  121.        << endl;
  122.   cout << "To REMOVE a number          :  "
  123.        << "READY>  r  <name>"
  124.        << endl;
  125.   cout << "To Print                    :  "
  126.        << "READY>  p"
  127.        << endl;
  128.   cout << "To QUIT                     :  "
  129.        << "READY>  q"
  130.        << endl;
  131.   cout << "To repeat these instructions:  "
  132.        << "READY>  ?"
  133.        << endl
  134.        << endl;
  135. }
  136. //--------------------------------------------------------------
  137.