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

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