CPP
47
tes.cpp
Guest on 5th August 2022 01:34:30 AM
#include "stdstuff.h"
#include "IntBag.h"
void realMain () {
IntBag bag;
int option, value;
bool valueObtained;
for (;;) {
cout << "\nEnter an option.\n"
<< " 1 - add value to bag\n"
<< " 2 - remove value from bag\n"
<< " 3 - count occurrences of a value\n"
<< " 4 - get # of values in the bag\n"
<< " 5 - list all values in bag\n"
<< " 6 - quit\n"
<< "Option: ";
cin >> option;
switch (option) {
case 1:
cout << "\nEnter value: ";
cin >> value;
if (bag.add(value)) {
cout << "Value successfully added to bag.\n";
} else {
cout << "Operation failed (bag full).\n";
}
break;
case 2:
cout << "\nEnter value: ";
cin >> value;
if (bag.remove(value)) {
cout << "Value successfully removed from bag.\n";
} else {
cout << "Operation failed (value was not in bag).\n";
}
break;
case 3:
cout << "\nEnter value: ";
cin >> value;
cout << "The bag contains " << bag.countOccurrences(value)
<< " occurences of that value.\n";
break;
case 4:
cout << "\nThe bag contains " << bag.size() << " values.\n";
break;
case 5:
cout << "\nThe bag contains:";
for (valueObtained = bag.startWalk(value);
valueObtained;
valueObtained = bag.continueWalk(value)) {
cout << " " << value;
}
cout << endl;
break;
case 6:
return;
default:
cout << "\nInvalid option ignored.\n";
break;
}
}
}
int main () {
try {
realMain ();
}
catch (exception &e) { // catches all uncaught exceptions
cout << "\nException <" << e.what() << "> occurred.\n";
}
pause ();
return 0;
}