Having trouble with menu

I am making a program that asks the user to choose a selection off of a menu, if the choose "1", an addition problem is displayed. If they enter -1, it should revert to the menu. I have been messing with my if structure and trying to figure out exactly how to do it, but I am stuck. (beginner c++) Right now, it simply says "Try Again!", as it should when the user enters any wrong number except -1.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Unit 6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;


int main()
{
	string operation;
	int menu;
	int numOne = (rand() % 9) + 1;
	int numTwo = (rand() % 9) + 1;
	int guess = -1;
	int answer;


	if (guess = -1)
		cout << "Welcome To The Aritmetic Quiz" << endl;
		cout << "Press 1 for Addition" << endl;
		cout << "Press 2 for Subtraction" << endl;
		cout << "Press 3 for Multiplication" << endl;
		cout << "Press 4 for Division" << endl;
		cout << "Press 5 for Modulus" << endl;
		cout << "Press 6 to Exit" << endl;
		cin >> menu;
		guess = 0;

	while (guess = -1)
	{

		if (menu != 6)
			switch (menu)
			{
			case 1:
				operation = "plus";
				answer = numOne + numTwo;
				while (guess = answer)
				{
					cout << "What is: " << " " << numOne << " " << operation << " " << numTwo << "?";
					cin >> guess;
					if (guess != answer)
					{
						cout << "Try again!" << endl;
					}
					else
					{
						cout << "Correct!";
						numOne = (rand() % 9) + 1;
						numTwo = (rand() % 9) + 1;
						answer = numOne + numTwo;
					}
				}
				break;

			case 2:
				operation = "minus";
				answer = numOne - numTwo;
				while (guess = answer)
				{
					cout << "What is: " << " " << numOne << " " << operation << " " << numTwo << "?";
					cin >> guess;
					if (guess = -1)
					if (guess != answer)
					{
						cout << "Try again!" << endl;
					}
					else
					{
						cout << "Correct!";
					}
					numOne = (rand() % 9) + 1;
					numTwo = (rand() % 9) + 1;
					answer = numOne - numTwo;
				}
				break;

			case 3:
				operation = "times";
				break;

			case 4:
				operation = "divided by";
				break;

			case 5:
				operation = "divided by";
				break;
			}
		else
			cout << "Come back soon!" << endl;
			break;
	}







	return 0;
}

Last edited on
= is not the same as ==
Look at your if statements.
= is for assignment
== tests for equality. Replace your = operators with == in your if statements.

Also, you are not using braces { } to form blocks around your if statements. This will cause the if statement to only be associated with the first command after the if statement.

1
2
3
4
if (a)
    b(); // b will be executed only if a is true
c(); // c will always be executed
d(); // d will always be executed 


1
2
3
4
5
if (a) {
    b(); // b will be executed only if a is true
    c(); // c will be executed only if a is true
}
d(); // d will always be executed 
Last edited on
Topic archived. No new replies allowed.