Really need some help making a menu


figured it out
Last edited on
Since you are going to generate random numbers in your program, you should include the header #include <time.h>
ok so i have made edits to your code which i have marked with comments.

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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{

srand(time(NULL));
//Try to declare variables at beginning and then just edit them throughout the program.
bool run = true;//this will handle the loop
int num1;
int num2;
int sum, answer;

while (run == true)//loop will run while run is true
{
cout << "A Addition" << endl;
cout << "S Subtraction" << endl;
cout << "Q Quit" << endl;
cout << "Please enter your choice: ";

char choice;
cin >> choice;


	switch(choice) //only need one switch(choice)
	{
	case 'A':
	case 'a':
	
		num1 = rand() % 100 + 1;
		num2 = rand() % 100 + 1;
		cout << num1 << endl;
		cout << num2 << endl;
		cout << " -----" << endl;

		int answer;
		cin >> answer;
		cin.ignore(100, 10);

		sum = num1 + num2;



		if (answer == sum)
		cout << "Congratulations, you got the right answer!" << endl;

		else
		{
		cout << endl;
		cout << "Sorry, that was wrong :(. Correct solution is" << endl;
		cout << num1 << endl;
		cout << num2 << endl;
		cout << " -----" << endl;
		cout << sum << endl;
		}

		cout << endl;
		break; // always add breaks at end
	case 'S':
	case 's':
		num1 = rand() % 100 + 1;
		num2 = rand() % 100 + 1;
		cout << num1 << endl;
		cout << num2 << endl;
		cout << " -----" << endl;

		
		cin >> answer;
		cin.ignore(100, 10);

		sum = num1 - num2;


		if (answer == sum)
		cout << "Congratulations, you got the right answer!" << endl;

		else
		{

			cout << endl;
			cout << "Sorry, that was wrong :(. Correct solution is" << endl;
			cout << num1 << endl;
			cout << num2 << endl;
			cout << " -----" << endl;
			cout << sum << endl;
		}
		cout << endl;
		break;

	case 'Q':
	case 'q': 
		run = false; //will make run false which will end the loop
		break;
	default: // this will make it so if they enter something that is not a A s S q Q then it will yell at them and keep the loop going
		cout << "Please reread and reenter answer!" << endl;
		break;
	}
	}

return 0;//always add return o; to end program
}
FYI run==true is the same as run.

Try to declare variables at beginning and then just edit them throughout the program.


Bad. Declare variables where you need them. You'll also want to use getline rather than cin>>, otherwise you'll leave unconsumed input if the user happens to enter more than one character.
during a switch case statement declaring the variables during the case gives an error of

initialization of 'variable' is skipped by case label

that is why i said to declare before. I was talking about this program in particular in larger projects and others not being a switch case yes it is smarter to declare as you need.

and i was using run==true to show that the variable must be initialized to true i don't know how much programming this person has had and i thought it would make it easier to follow for them

and he/she has protected against extra characters using cin.ignore() so getline() is not necessary
Last edited on
i need it to loop until they answer it correctly
Topic archived. No new replies allowed.