CPP   47

tes.cpp

Guest on 5th August 2022 01:34:30 AM

  1. #include "stdstuff.h"
  2.  
  3. #include "IntBag.h"
  4.  
  5.  
  6. void realMain () {
  7.  
  8.   IntBag bag;
  9.   int option, value;
  10.   bool valueObtained;
  11.  
  12.   for (;;) {
  13.  
  14.     cout << "\nEnter an option.\n"
  15.          << "  1 - add value to bag\n"
  16.          << "  2 - remove value from bag\n"
  17.          << "  3 - count occurrences of a value\n"
  18.          << "  4 - get # of values in the bag\n"
  19.          << "  5 - list all values in bag\n"
  20.          << "  6 - quit\n"
  21.          << "Option: ";
  22.     cin >> option;
  23.  
  24.     switch (option) {
  25.  
  26.     case 1:
  27.  
  28.       cout << "\nEnter value: ";
  29.       cin >> value;
  30.       if (bag.add(value)) {
  31.         cout << "Value successfully added to bag.\n";
  32.       } else {
  33.         cout << "Operation failed (bag full).\n";
  34.       }
  35.       break;
  36.  
  37.     case 2:
  38.  
  39.       cout << "\nEnter value: ";
  40.       cin >> value;
  41.       if (bag.remove(value)) {
  42.         cout << "Value successfully removed from bag.\n";
  43.       } else {
  44.         cout << "Operation failed (value was not in bag).\n";
  45.       }
  46.       break;
  47.  
  48.     case 3:
  49.  
  50.       cout << "\nEnter value: ";
  51.       cin >> value;
  52.       cout << "The bag contains " << bag.countOccurrences(value)
  53.            << " occurences of that value.\n";
  54.       break;
  55.  
  56.     case 4:
  57.  
  58.       cout << "\nThe bag contains " << bag.size() << " values.\n";
  59.       break;
  60.  
  61.     case 5:
  62.  
  63.       cout << "\nThe bag contains:";
  64.       for (valueObtained = bag.startWalk(value);
  65.            valueObtained;
  66.            valueObtained = bag.continueWalk(value)) {
  67.         cout << " " << value;
  68.       }
  69.       cout << endl;
  70.       break;
  71.      
  72.     case 6:
  73.  
  74.       return;
  75.  
  76.     default:
  77.  
  78.       cout << "\nInvalid option ignored.\n";
  79.       break;
  80.  
  81.     }
  82.  
  83.   }
  84.  
  85. }
  86.  
  87.  
  88.  
  89. int main () {
  90.  
  91.   try {
  92.  
  93.     realMain ();
  94.  
  95.   }
  96.   catch (exception &e) { // catches all uncaught exceptions
  97.  
  98.     cout << "\nException <" << e.what() << "> occurred.\n";
  99.  
  100.   }
  101.  
  102.   pause ();
  103.   return 0;
  104.  
  105. }

Raw Paste


Login or Register to edit or fork this paste. It's free.