help with menus!

Im trying to write a program that will make a simple math quiz for students using random numbers. The program should display a menu that allows the user to choose between Addition, Subtraction, Multiplication, Division, and Modulus operations. Here is what I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int main()
{



	char choice;
	int n1,n2,answer,useranswer;
	bool quitnow = false;

	srand(static_cast<unsigned>(time(NULL)));
	n1 = rand()%20+1;
	n2 = rand()%20+1;
	answer = n1 = n2;

	
	{
	cout << "a.Find the sum of two numbers\n";
	cout << "b.Subtract two numbers\n";
	cout << "m.find the product of two numbers\n";
	cout << "d.Divide two numbers\n";
	cout << "r. find the Modulus of two numbers\n";
	cout << "q.Quit the program\n";
	cin >> choice;
	}

	if(choice == "a")
	{
		cout << "what is" << n1 << "+" << n2 << "?";
		cin >> useranswer;
	}
	else if (choice == "b")
	{
		cout << "what is" << n1 << "-" << n2 << "?";
		cin >> useranswer;
	}
	else if (choice == "m")
	{
		cout << "what is" << n1 << "*" << n2 << "?";
		cin >> useranswer;
	}
	else if(choice == "d")
	{
		cout << "what is" << n1 << "/" << n2 << "?";
		cin >> useranswer;
	}
	else if(choice == "r")
	{
		cout << "what is" << n1 << "%" << n2 << "?";
		cin >> useranswer;
	}
	else if (choice == "q")
	{
		quitnow = true;
	}
	return 0;
}
Last edited on
Please use the code tags when you post code. On the right of where you enter your post, look for <>.

You haven't really been specific as to what you're stuck on.

Your do-while loop is missing the while statement.
1
2
3
4
5
6
7
8
9
10
do
{
    cout << "a.Find the sum of two numbers\n";
    cout << "b.Subtract two numbers\n";
    cout << "m.find the product of two numbers\n";
    cout << "d.Divide two numbers\n";
    cout << "r. find the Modulus of two numbers\n";
    cout << "q.Quit the program\n";
    cin >> choice;
}while( /* condition here */ );


Maybe take a look at a switch-statement. It will be a lot more readable that a lot of if-statements.
http://www-numi.fnal.gov/computing/minossoft/releases/R2.3/WebDocs/Companion/cxx_crib/switch.html

Hope this helps. (:
Topic archived. No new replies allowed.