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

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