Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cs-142:allowance-with-menu [2016/10/07 11:35]
kseppi
cs-142:allowance-with-menu [2016/11/02 17:25]
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>​
Line 136: Line 138:
 } }
  
 +</​code>​
 +
 +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.
 +
 +<code cpp>
 +#include <​iostream>​
 +#include <​limits>​
 +#include <​stdlib.h>​
 +#include <​time.h>​
 +#include <​iomanip>​
 +#include <​tgmath.h>​
 +#include <​limits>​
 +
 +using namespace std;
 +
 +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'​);​
 +
 +                ​
 +                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;
 +        }
 +        cout << endl;
 +        ​
 +    }
 +    ​
 +    //​system("​pause"​);​
 +    ​
 +    return 0;
 +}
 </​code>​ </​code>​
cs-142/allowance-with-menu.txt ยท Last modified: 2016/11/02 17:25 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