Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cs-142:allowance-with-menu [2014/10/02 08:32]
kseppi created
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() { 
- srand(time(0)); +    const int STOP_TESTS = 0; 
- string ans+    const int INVALID_CHOICE = -1; 
- +    const int TEST_LINC_AND_MAX = 1; 
- cout << "\"Q\" ​-- quit\n"; +    const int TEST_CINDY_AND_RANDI = 2; 
- cout << "\"C\" -- compute\n"; +    const int MONTHS_IN_YEAR = 12; 
- cout << ​"​\"​X\" -- say something nice\n"; +    const double LINC_INITIAL = 0.10; 
- cout << "what do you want me to do? "; +    const double MAX_INITIAL = 0.01; 
- getline(cin,ans)+    const double RANDI_MIN = 0.30; 
- while((ans != "​Q"​) && (ans != "​q"​)) { +    const double RANDI_MAX = 5.0; 
- +    double lincsTotal = 0; 
- if ((ans == "​C"​|| (ans == "​c"​)) { +    double maxsTotal = 0; 
- double duanestotal = 0.01; +    double lincsAverage = 0; 
- double kevinstotal ​0.1+    double maxsAverage = 0; 
- double cindystotal ​0.1+    double cindisTotal = 0; 
- +    double randisTotal = 0; 
- double duanescurrent ​0.01+     
- double kevinscurrent ​0.10+    double lincsCurrent = 0; 
- double cindyscurrent = 0.10+    double maxsCurrent = 0; 
- +    double cindisCurrent = 0; 
- +    double randisCurrent = 0; 
- for (int i = 1; i <= 12; i++) { +    int cindiMoreThanRandi = 0; 
- +     
- cout << "​For ​the month " << i << endl; +    int choice = 0; 
- cout << "kevins ​allowance this month = " << ​kevinscurrent ​<< " and running total " << ​kevinstotal ​<< endl;  +     
- cout << "duanes ​allowance this month = " << ​duanescurrent ​<< " and running total " << ​duanestotal ​<< endl << endl;  +    ​srand(42); // for now, do the same random thing every time 
- cout << "​cindys allowance this month = " << cindyscurrent << " and running total " << cindystotal << endl << endl;  +    //srand(time(NULL)); 
-  +    cout << fixed << setprecision(2)
- // get ready for the next month... +     
- duanescurrent ​*= 2; +    cout << "Sarting tool for simulating allowances" ​<< endl; 
- kevinscurrent ​+= 0.10; +     
- cindyscurrent = cindyscurrent+double(rand()%11-5)/100.0; +    while (true) { 
- +         
- duanestotal ​+= duanescurrent+        cout << endl; 
- kevinstotal ​+= kevinscurrent+        cout << ​"MENU: Please select one of the following options:" ​<< endl
- cindystotal +cindyscurrent+        cout << endl; 
- } +        cout << STOP_TESTS ​<< " ​- Quit the program" ​<< endl; 
-  +        cout << TEST_LINC_AND_MAX << ​" - Simulate allowance for Linc and Max" ​<< endl
- +        cout << ​TEST_CINDY_AND_RANDI << ​" - Simulate allowance for Cindi and Randi" ​<< endl
- else if ((ans == "​S"​|| (ans == "s")) { +        cout << endl; 
- cout << "Something nice.\n"; +         
- } +        ​cout << "Enter your selection now: "; 
- else { +        cin >> choice
- cout << "invalid input, try again.\n"; +        ​cin.ignore(numeric_limits<​streamsize>::​max(), '​\n'​); 
- +        cout << endl; 
- +         
- cout << "​\"​Q\" ​-- quit\n"; +        ​if (choice ​== STOP_TESTS
- cout << "​\"​C\" ​-- compute\n"; +            break; 
- cout << "\"X\" ​-- say something nice.\n"; +        } 
- cout << "what do you want me to do? "; +        else if (choice ​== 1) { 
- //​getline(cin,​ans);​ +            // We have to reset these every time we pick this option 
- cin >> ans+            ​lincsCurrent ​LINC_INITIAL
-+            ​maxsCurrent ​MAX_INITIAL
- //system("​pause"​);​ +            ​lincsTotal ​lincsCurrent
- return 0;+            ​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 (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 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 <​stdlib.h>
 #include <​time.h>​ #include <​time.h>​
 +#include <​iomanip>​
 +#include <​tgmath.h>​
 +#include <​limits>​
  
 using namespace std; using namespace std;
  
-int main () { +int main() { 
- srand(time(0)); +    const int MONTHS_IN_YEAR = 12; 
- string ans; +    const double LINC_INITIAL = 0.10; 
- +    const double MAX_INITIAL = 0.01; 
- cout << ​"​\"​0\"​ -- quit\n"​+    const double RANDI_MIN = 0.30; 
- cout << "​\"​1\" ​-- compute\n"; +    const double RANDI_MAX = 5.0; 
- cout << ​"​\"​2\"​ -- say something nice\n"​+    double lincsTotal = 0; 
- cout << "what do you want me to do? "; +    double maxsTotal = 0; 
- getline(cin,​ans); +    double lincsAverage = 0; 
- while((ans != "0")) { +    double maxsAverage = 0; 
- +    double cindisTotal = 0; 
- if ((ans == "1")) { +    double randisTotal = 0; 
- double duanestotal = 0.01+     
- double kevinstotal = 0.1+    double lincsCurrent = 0; 
- double cindystotal = 0.1+    double maxsCurrent = 0; 
- +    double cindisCurrent = 0; 
- double duanescurrent ​0.01+    double randisCurrent = 0; 
- double kevinscurrent ​0.10+    int cindiMoreThanRandi = 0; 
- double cindyscurrent ​0.10+     
- +    string choice = "";​ 
- +     
- for (int i = 1; i <= 12; i++) { +    ​srand(42); // for now, do the same random thing every time 
- +    //srand(time(NULL)); 
- cout << "​For ​the month " << i << endl; +    cout << ​fixed << setprecision(2)
- cout << "kevins ​allowance this month = " << ​kevinscurrent ​<< " and running total " << ​kevinstotal ​<< endl;  +     
- cout << "duanes ​allowance this month = " << ​duanescurrent ​<< " and running total " << ​duanestotal ​<< endl << endl;  +    ​cout << "Tool for simulating allowances - type \"list\" ​to see available choices" ​<< endl
- cout << "​cindys allowance this month = " << cindyscurrent << " and running total " << cindystotal << endl << endl;  +    cout << ​endl
-  +    while (true) { 
- // get ready for the next month... +        ​cout << "Choice: ​"; 
- duanescurrent ​*= 2; +        getline(cin,​choice); 
- kevinscurrent ​+= 0.10; +        cout << endl; 
- cindyscurrent = cindyscurrent+double(rand()%11-5)/100.0; +         
- +        if ("quit" ​== choice) { 
- duanestotal ​+= duanescurrent+            ​break;​ 
- kevinstotal ​+= kevinscurrent+        } 
- cindystotal +cindyscurrent+        else if ("list" ​== choice) { 
- } +            cout << endl
-  +            cout << "​Program options are:" << endl
- +            cout << endl
- else if ((ans == "2")) { +            cout << "quit - Quit the program"​ << endl; 
- cout << "Something nice.\n"; +            cout << "lm - Simulate allowance for Linc and Max" << endl; 
- +            cout << "cr - Simulate allowance for Cindi and Randi" << endl; 
- else ​+        } 
- cout << "invalid input, try again.\n"; +         
- }+        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');
  
- cout << "\"0\" ​-- quit\n"; +                 
- cout << "​\"​1\" ​-- sing\n"; +                ​cout << "For month " ​<< i << endl; 
- cout << "\"2\" ​-- say something nice.\n"; +                cout << ​"Cindi\'s allowance this month = " ​<< cindisCurrent << " and running total " << cindisTotal << endl
- cout << "what do you want me to do? "; +                cout << "Randi\'s allowance this month = " ​<< randisCurrent << ​" ​and running total " ​<< randisTotal << endl << endl
- getline(cin,​ans)+                 
-+                 
- //​system("​pause"​);​ +                cindisTotal += cindisCurrent;​ 
- return 0;+                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
 +        } 
 +        cout << endl
 +         
 +    ​
 +     
 +    ​//​system("​pause"​);​ 
 +     
 +    ​return 0;
 } }
 </​code>​ </​code>​
cs-142/allowance-with-menu.1412260338.txt.gz · Last modified: 2014/10/02 08:32 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0