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.

#include <iostream>
#include <limits>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <tgmath.h>
 
using namespace std;
 
int main() {
    const int STOP_TESTS = 0;
    const int INVALID_CHOICE = -1;
    const int TEST_LINC_AND_MAX = 1;
    const int TEST_CINDY_AND_RANDI = 2;
    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;
 
    int choice = 0;
 
    srand(42); // for now, do the same random thing every time
    //srand(time(NULL));
    cout << fixed << setprecision(2);
 
    cout << "Sarting tool for simulating allowances" << endl;
 
    while (true) {
 
        cout << endl;
        cout << "MENU: Please select one of the following options:" << endl;
        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;
        cout << endl;
 
        cout << "Enter your selection now: ";
        cin >> choice;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << endl;
 
        if (choice == STOP_TESTS) {
            break;
        }
        else if (choice == 1) {
            // 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 (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;
}

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.

#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;
}
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