**This is an old revision of the document!**


#include <iostream>
#include <string>
#include <time.h>
 
using namespace std;
 
int main() {
 
	srand(time(0));
	string ans;
 
	do {
		cout << "\"Q\" -- quit\n";
		cout << "\"C\" -- compute\n";
		cout << "\"S\" -- say something nice\n";
		cout << "what do you want me to do? ";
		cin >> ans;
		// getline(cin, ans);
 
		if ((ans == "C") || (ans == "c")) {
			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 == "S") || (ans == "s")) {
			cout << "Something nice.\n";
		}
		else if ((ans == "Q") || (ans == "q")) {
			cout << "Ok, bye\n";
		}
		else {
			cout << "invalid input, try again.\n";
		}
 
 
	} while ((ans != "Q") && (ans != "q"));
 
	//system("pause");
	return 0;
}

Here it is with a numeric menu

This will loop forever on bad int's.

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

Here it is with a more cleaver numeric menu

Note the use of a string

#include <iostream>
#include <string>
#include <time.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;
}
cs-142/allowance-with-menu.1412260930.txt.gz · Last modified: 2014/10/02 08:42 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