This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
cs-142:allowance-with-menu [2014/10/02 09:22] kseppi |
cs-142:allowance-with-menu [2016/11/02 17:25] (current) kseppi |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | This version has a numeric menu, there is an other example after it that uses a string for the choices in the menu. Please take a look at both. | ||
+ | |||
<code cpp> | <code cpp> | ||
#include <iostream> | #include <iostream> | ||
- | #include <string> | + | #include <limits> |
+ | #include <stdlib.h> | ||
#include <time.h> | #include <time.h> | ||
+ | #include <iomanip> | ||
+ | #include <tgmath.h> | ||
using namespace std; | using namespace std; | ||
int main() { | int main() { | ||
- | + | const int STOP_TESTS = 0; | |
- | srand(time(0)); | + | const int INVALID_CHOICE = -1; |
- | string ans; | + | const int TEST_LINC_AND_MAX = 1; |
- | + | const int TEST_CINDY_AND_RANDI = 2; | |
- | do { | + | const int MONTHS_IN_YEAR = 12; |
- | cout << "\"Q\" -- quit\n"; | + | const double LINC_INITIAL = 0.10; |
- | cout << "\"C\" -- compute\n"; | + | const double MAX_INITIAL = 0.01; |
- | cout << "\"S\" -- say something nice\n"; | + | const double RANDI_MIN = 0.30; |
- | cout << "what do you want me to do? "; | + | const double RANDI_MAX = 5.0; |
- | cin >> ans; | + | double lincsTotal = 0; |
- | // getline(cin, ans); | + | double maxsTotal = 0; |
- | + | double lincsAverage = 0; | |
- | if ((ans == "C") || (ans == "c")) { | + | double maxsAverage = 0; |
- | double duanestotal = 0.01; | + | double cindisTotal = 0; |
- | double kevinstotal = 0.1; | + | double randisTotal = 0; |
- | double cindystotal = 0.1; | + | |
- | + | double lincsCurrent = 0; | |
- | double duanescurrent = 0.01; | + | double maxsCurrent = 0; |
- | double kevinscurrent = 0.10; | + | double cindisCurrent = 0; |
- | double cindyscurrent = 0.10; | + | double randisCurrent = 0; |
- | + | int cindiMoreThanRandi = 0; | |
- | + | ||
- | for (int i = 1; i <= 12; i++) { | + | int choice = 0; |
- | + | ||
- | cout << "For the month " << i << endl; | + | srand(42); // for now, do the same random thing every time |
- | cout << "kevins allowance this month = " << kevinscurrent << " and running total " << kevinstotal << endl; | + | //srand(time(NULL)); |
- | cout << "duanes allowance this month = " << duanescurrent << " and running total " << duanestotal << endl << endl; | + | cout << fixed << setprecision(2); |
- | cout << "cindys allowance this month = " << cindyscurrent << " and running total " << cindystotal << endl << endl; | + | |
- | + | cout << "Sarting tool for simulating allowances" << endl; | |
- | // get ready for the next month... | + | |
- | duanescurrent *= 2; | + | while (true) { |
- | kevinscurrent += 0.10; | + | |
- | cindyscurrent = cindyscurrent + double(rand() % 11 - 5) / 100.0; | + | cout << endl; |
- | + | cout << "MENU: Please select one of the following options:" << endl; | |
- | duanestotal += duanescurrent; | + | cout << endl; |
- | kevinstotal += kevinscurrent; | + | cout << STOP_TESTS << " - Quit the program" << endl; |
- | cindystotal += cindyscurrent; | + | cout << TEST_LINC_AND_MAX << " - Simulate allowance for Linc and Max" << endl; |
- | } | + | cout << TEST_CINDY_AND_RANDI << " - Simulate allowance for Cindi and Randi" << endl; |
- | + | cout << endl; | |
- | } | + | |
- | else if ((ans == "S") || (ans == "s")) { | + | cout << "Enter your selection now: "; |
- | cout << "Something nice.\n"; | + | cin >> choice; |
- | } | + | cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
- | else if ((ans == "Q") || (ans == "q")) { | + | cout << endl; |
- | cout << "Ok, bye\n"; | + | |
- | } | + | if (choice == STOP_TESTS) { |
- | else { | + | break; |
- | cout << "invalid input, try again.\n"; | + | } |
- | } | + | else if (choice == 1) { |
- | + | // We have to reset these every time we pick this option | |
- | + | lincsCurrent = LINC_INITIAL; | |
- | } while ((ans != "Q") && (ans != "q")); | + | maxsCurrent = MAX_INITIAL; |
- | + | lincsTotal = lincsCurrent; | |
- | //system("pause"); | + | maxsTotal = maxsCurrent; |
- | return 0; | + | |
+ | cout << "Simulate allowance for Linc and Max:" << endl; | ||
+ | for (int i = 1; i <= MONTHS_IN_YEAR; i++) { | ||
+ | |||
+ | cout << "For month " << i << endl; | ||
+ | cout << "Linc\'s allowance this month = " << lincsCurrent << " and running total " << lincsTotal << endl; | ||
+ | cout << "Max\'s allowance this month = " << maxsCurrent << " and running total " << maxsTotal << endl << endl; | ||
+ | |||
+ | // get ready for the next month... | ||
+ | maxsCurrent *= 2; // max get's doubled | ||
+ | lincsCurrent += 0.10; // linc gets incremented by 0.1 | ||
+ | |||
+ | maxsTotal += maxsCurrent; | ||
+ | lincsTotal += lincsCurrent; | ||
+ | } | ||
+ | |||
+ | lincsAverage = lincsTotal/MONTHS_IN_YEAR; | ||
+ | maxsAverage = maxsTotal/MONTHS_IN_YEAR; | ||
+ | |||
+ | cout << "Summary: " << endl; | ||
+ | cout << "Linc\'s average allowance: " << lincsAverage << endl; | ||
+ | cout << "Max\'s average allowance: " << maxsAverage << endl; | ||
+ | |||
+ | cout << endl; | ||
+ | |||
+ | } | ||
+ | else if (choice == 2) { | ||
+ | // We have to reset these every time we pick this option | ||
+ | cindisTotal = 0; | ||
+ | randisTotal = 0; | ||
+ | cindiMoreThanRandi = 0; | ||
+ | |||
+ | cout << "Simulate allowance for Cindi and Randi:" << endl; | ||
+ | for (int i = 1; i <= MONTHS_IN_YEAR; i++) { | ||
+ | |||
+ | // get ready for the next month... | ||
+ | // randomly choose a value for Randi | ||
+ | randisCurrent = RANDI_MIN + ((RANDI_MAX - RANDI_MIN) * (rand() / (static_cast <double>(RAND_MAX)))); | ||
+ | |||
+ | // Ask for the value from cin for cindi | ||
+ | cout << "What value should I use for Cindi's allowance? "; | ||
+ | cin >> cindisCurrent; | ||
+ | |||
+ | cout << "For month " << i << endl; | ||
+ | cout << "Cindi\'s allowance this month = " << cindisCurrent << " and running total " << cindisTotal << endl; | ||
+ | cout << "Randi\'s allowance this month = " << randisCurrent << " and running total " << randisTotal << endl << endl; | ||
+ | |||
+ | |||
+ | cindisTotal += cindisCurrent; | ||
+ | randisTotal += randisCurrent; | ||
+ | if (cindisCurrent > randisCurrent) { | ||
+ | cindiMoreThanRandi++; | ||
+ | } | ||
+ | } | ||
+ | cout << "Summary: " << endl; | ||
+ | cout << "Cindi got more than Randi " << cindiMoreThanRandi << " times" << endl; | ||
+ | cout << endl; | ||
+ | } | ||
+ | else { | ||
+ | cout << "INVALID SELECTION. Please enter a valid option." << endl; | ||
+ | } | ||
+ | |||
+ | choice = INVALID_CHOICE; | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | system("pause"); | ||
+ | |||
+ | return 0; | ||
} | } | ||
- | </code> | ||
- | =Here it is with a numeric menu= | ||
- | |||
- | This will loop forever on bad int's. | ||
- | |||
- | <code cpp> | ||
- | #include <iostream> | ||
- | #include <string> | ||
- | #include <time.h> | ||
- | |||
- | using namespace std; | ||
- | |||
- | int main () { | ||
- | srand(time(0)); | ||
- | int ans; | ||
- | |||
- | cout << "\"0\" -- quit\n"; | ||
- | cout << "\"1\" -- compute\n"; | ||
- | cout << "\"2\" -- say something nice\n"; | ||
- | cout << "what do you want me to do? "; | ||
- | cin >> ans; | ||
- | while((ans != 0)) { | ||
- | |||
- | if ((ans == 1)) { | ||
- | double duanestotal = 0.01; | ||
- | double kevinstotal = 0.1; | ||
- | double cindystotal = 0.1; | ||
- | |||
- | double duanescurrent = 0.01; | ||
- | double kevinscurrent = 0.10; | ||
- | double cindyscurrent = 0.10; | ||
- | |||
- | |||
- | for (int i = 1; i <= 12; i++) { | ||
- | |||
- | cout << "For the month " << i << endl; | ||
- | cout << "kevins allowance this month = " << kevinscurrent << " and running total " << kevinstotal << endl; | ||
- | cout << "duanes allowance this month = " << duanescurrent << " and running total " << duanestotal << endl << endl; | ||
- | cout << "cindys allowance this month = " << cindyscurrent << " and running total " << cindystotal << endl << endl; | ||
- | |||
- | // get ready for the next month... | ||
- | duanescurrent *= 2; | ||
- | kevinscurrent += 0.10; | ||
- | cindyscurrent = cindyscurrent+double(rand()%11-5)/100.0; | ||
- | |||
- | duanestotal += duanescurrent; | ||
- | kevinstotal += kevinscurrent; | ||
- | cindystotal += cindyscurrent; | ||
- | } | ||
- | |||
- | } | ||
- | else if ((ans == 2)) { | ||
- | cout << "Something nice.\n"; | ||
- | } | ||
- | else { | ||
- | cout << "invalid input, try again.\n"; | ||
- | } | ||
- | |||
- | cout << "\"0\" -- quit\n"; | ||
- | cout << "\"1\" -- sing\n"; | ||
- | cout << "\"2\" -- say something nice.\n"; | ||
- | cout << "what do you want me to do? "; | ||
- | cin >> ans; | ||
- | } | ||
- | //system("pause"); | ||
- | return 0; | ||
- | } | ||
</code> | </code> | ||
- | =Here it is with a more cleaver numeric menu= | + | Here is a version that uses strings for the choices, this is convenient because I can just use the string instead of a number that implies the option name. |
- | + | ||
- | Note the use of a string | + | |
<code cpp> | <code cpp> | ||
#include <iostream> | #include <iostream> | ||
- | #include <string> | + | #include <limits> |
- | #include <time.h> | + | #include <stdlib.h> |
- | + | ||
- | using namespace std; | + | |
- | + | ||
- | int main () { | + | |
- | srand(time(0)); | + | |
- | string ans; | + | |
- | + | ||
- | cout << "\"0\" -- quit\n"; | + | |
- | cout << "\"1\" -- compute\n"; | + | |
- | cout << "\"2\" -- say something nice\n"; | + | |
- | cout << "what do you want me to do? "; | + | |
- | getline(cin,ans); | + | |
- | while((ans != "0")) { | + | |
- | + | ||
- | if ((ans == "1")) { | + | |
- | double duanestotal = 0.01; | + | |
- | double kevinstotal = 0.1; | + | |
- | double cindystotal = 0.1; | + | |
- | + | ||
- | double duanescurrent = 0.01; | + | |
- | double kevinscurrent = 0.10; | + | |
- | double cindyscurrent = 0.10; | + | |
- | + | ||
- | + | ||
- | for (int i = 1; i <= 12; i++) { | + | |
- | + | ||
- | cout << "For the month " << i << endl; | + | |
- | cout << "kevins allowance this month = " << kevinscurrent << " and running total " << kevinstotal << endl; | + | |
- | cout << "duanes allowance this month = " << duanescurrent << " and running total " << duanestotal << endl << endl; | + | |
- | cout << "cindys allowance this month = " << cindyscurrent << " and running total " << cindystotal << endl << endl; | + | |
- | + | ||
- | // get ready for the next month... | + | |
- | duanescurrent *= 2; | + | |
- | kevinscurrent += 0.10; | + | |
- | cindyscurrent = cindyscurrent+double(rand()%11-5)/100.0; | + | |
- | + | ||
- | duanestotal += duanescurrent; | + | |
- | kevinstotal += kevinscurrent; | + | |
- | cindystotal += cindyscurrent; | + | |
- | } | + | |
- | + | ||
- | } | + | |
- | else if ((ans == "2")) { | + | |
- | cout << "Something nice.\n"; | + | |
- | } | + | |
- | else { | + | |
- | cout << "invalid input, try again.\n"; | + | |
- | } | + | |
- | + | ||
- | cout << "\"0\" -- quit\n"; | + | |
- | cout << "\"1\" -- sing\n"; | + | |
- | cout << "\"2\" -- say something nice.\n"; | + | |
- | cout << "what do you want me to do? "; | + | |
- | getline(cin,ans); | + | |
- | } | + | |
- | //system("pause"); | + | |
- | return 0; | + | |
- | } | + | |
- | </code> | + | |
- | A dumb version: | + | |
- | <code cpp> | + | |
- | #include <iostream> | + | |
- | #include <string> | + | |
#include <time.h> | #include <time.h> | ||
+ | #include <iomanip> | ||
+ | #include <tgmath.h> | ||
+ | #include <limits> | ||
using namespace std; | using namespace std; | ||
int main() { | int main() { | ||
+ | const int MONTHS_IN_YEAR = 12; | ||
+ | const double LINC_INITIAL = 0.10; | ||
+ | const double MAX_INITIAL = 0.01; | ||
+ | const double RANDI_MIN = 0.30; | ||
+ | const double RANDI_MAX = 5.0; | ||
+ | double lincsTotal = 0; | ||
+ | double maxsTotal = 0; | ||
+ | double lincsAverage = 0; | ||
+ | double maxsAverage = 0; | ||
+ | double cindisTotal = 0; | ||
+ | double randisTotal = 0; | ||
+ | | ||
+ | double lincsCurrent = 0; | ||
+ | double maxsCurrent = 0; | ||
+ | double cindisCurrent = 0; | ||
+ | double randisCurrent = 0; | ||
+ | int cindiMoreThanRandi = 0; | ||
+ | | ||
+ | string choice = ""; | ||
+ | | ||
+ | srand(42); // for now, do the same random thing every time | ||
+ | //srand(time(NULL)); | ||
+ | cout << fixed << setprecision(2); | ||
+ | | ||
+ | cout << "Tool for simulating allowances - type \"list\" to see available choices" << endl; | ||
+ | cout << endl; | ||
+ | while (true) { | ||
+ | cout << "Choice: "; | ||
+ | getline(cin,choice); | ||
+ | cout << endl; | ||
+ | | ||
+ | if ("quit" == choice) { | ||
+ | break; | ||
+ | } | ||
+ | else if ("list" == choice) { | ||
+ | cout << endl; | ||
+ | cout << "Program options are:" << endl; | ||
+ | cout << endl; | ||
+ | cout << "quit - Quit the program" << endl; | ||
+ | cout << "lm - Simulate allowance for Linc and Max" << endl; | ||
+ | cout << "cr - Simulate allowance for Cindi and Randi" << endl; | ||
+ | } | ||
+ | | ||
+ | else if ("lm" == choice) { | ||
+ | // We have to reset these every time we pick this option | ||
+ | lincsCurrent = LINC_INITIAL; | ||
+ | maxsCurrent = MAX_INITIAL; | ||
+ | lincsTotal = lincsCurrent; | ||
+ | maxsTotal = maxsCurrent; | ||
+ | | ||
+ | cout << "Simulate allowance for Linc and Max:" << endl; | ||
+ | for (int i = 1; i <= MONTHS_IN_YEAR; i++) { | ||
+ | | ||
+ | cout << "For month " << i << endl; | ||
+ | cout << "Linc\'s allowance this month = " << lincsCurrent << " and running total " << lincsTotal << endl; | ||
+ | cout << "Max\'s allowance this month = " << maxsCurrent << " and running total " << maxsTotal << endl << endl; | ||
+ | | ||
+ | // get ready for the next month... | ||
+ | maxsCurrent *= 2; // max get's doubled | ||
+ | lincsCurrent += 0.10; // linc gets incremented by 0.1 | ||
+ | | ||
+ | maxsTotal += maxsCurrent; | ||
+ | lincsTotal += lincsCurrent; | ||
+ | } | ||
+ | | ||
+ | lincsAverage = lincsTotal/MONTHS_IN_YEAR; | ||
+ | maxsAverage = maxsTotal/MONTHS_IN_YEAR; | ||
+ | | ||
+ | cout << "Summary: " << endl; | ||
+ | cout << "Linc\'s average allowance: " << lincsAverage << endl; | ||
+ | cout << "Max\'s average allowance: " << maxsAverage << endl; | ||
+ | | ||
+ | cout << endl; | ||
+ | | ||
+ | } | ||
+ | else if ("cr" == choice) { | ||
+ | // We have to reset these every time we pick this option | ||
+ | cindisTotal = 0; | ||
+ | randisTotal = 0; | ||
+ | cindiMoreThanRandi = 0; | ||
+ | | ||
+ | cout << "Simulate allowance for Cindi and Randi:" << endl; | ||
+ | for (int i = 1; i <= MONTHS_IN_YEAR; i++) { | ||
+ | | ||
+ | // get ready for the next month... | ||
+ | // randomly choose a value for Randi | ||
+ | randisCurrent = RANDI_MIN + ((RANDI_MAX - RANDI_MIN) * (rand() / (static_cast <double>(RAND_MAX)))); | ||
+ | | ||
+ | // Ask for the value from cin for cindi | ||
+ | cout << "What value should I use for Cindi's allowance? "; | ||
+ | cin >> cindisCurrent; | ||
+ | cin.ignore(numeric_limits<streamsize>::max(), '\n'); | ||
- | srand(time(0)); | + | |
- | string ans; | + | cout << "For month " << i << endl; |
- | + | cout << "Cindi\'s allowance this month = " << cindisCurrent << " and running total " << cindisTotal << endl; | |
- | + | cout << "Randi\'s allowance this month = " << randisCurrent << " and running total " << randisTotal << endl << endl; | |
- | int duanestotal = 1; | + | |
- | int kevinstotal = 10; | + | |
- | int cindystotal = 10; | + | cindisTotal += cindisCurrent; |
- | + | randisTotal += randisCurrent; | |
- | int duanescurrent = 1; | + | if (cindisCurrent > randisCurrent) { |
- | int kevinscurrent = 10; | + | cindiMoreThanRandi++; |
- | int cindyscurrent = 10; | + | } |
- | + | } | |
- | int cindysinc = 0; | + | cout << "Summary: " << endl; |
- | + | cout << "Cindi got more than Randi " << cindiMoreThanRandi << " times" << endl; | |
- | + | cout << endl; | |
- | + | } | |
- | for (int i = 1; i <= 12; i++) { | + | else { |
- | + | cout << "INVALID SELECTION. Please enter a valid option." << endl; | |
- | + | } | |
- | cout << "For the month " << i << endl; | + | cout << endl; |
- | cout << "kevins allowance this month = " << kevinscurrent << " and running total " << kevinstotal << endl; | + | |
- | cout << "duanes allowance this month = " << duanescurrent << " and running total " << duanestotal << endl; | + | } |
- | cout << "cindys allowance this month = " << cindyscurrent << " and running total " << cindystotal << endl << endl; | + | |
- | + | //system("pause"); | |
- | // get ready for the next month... | + | |
- | duanescurrent *= 2; | + | return 0; |
- | kevinscurrent += 10; | + | |
- | cindysinc = rand() % 11 + 5; | + | |
- | cindyscurrent = cindyscurrent + cindysinc; | + | |
- | + | ||
- | + | ||
- | + | ||
- | duanestotal += duanescurrent; | + | |
- | kevinstotal += kevinscurrent; | + | |
- | cindystotal += cindyscurrent; | + | |
- | } | + | |
- | + | ||
- | system("pause"); | + | |
- | return 0; | + | |
} | } | ||
</code> | </code> |