Problems with menu

Hello everyone,

I'm new on this forum and I would not come here to ask for help if I'm not desperate. I just learn loop last week and my professor give me a project to create a menu for a math tutor program. The user will then select what do they want to do. The program will seed 2 random numbers from 200 to 999 if the user select addition or subtraction. If the user select division or multiplication, the second number will be seeded again from 0 to 10. Here's my code.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
///This program belongs to Nghi Tran
// C.S. M10A Fall 2014- Sec. 71245
// Lab06A: Math Tutor with menu options

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{	//Introduction to user
	cout << "Welcome to Math Tutor. This program is designed to help student" << endl;
	cout << "to learn how to do basic math such as addition, subtraction,..." << endl;
	cout << endl;

	//Display my name header on the screen
	cout << "******************************" << endl;
	cout << "           Nghi Tran          " << endl;
	cout << "      C.S M10A Fall 2014      " << endl;
	cout << "   Programming Assignment 6   " << endl;
	cout << "   Due Date: Thurs. Oct. 2    " << endl;
	cout << "******************************" << endl;
	cout << endl;

	//Declare variables
	char choice;
	int num1, num2, answer, useranswer, remainder, userremainder;

	//Seed 2 random numbers
	srand(static_cast<unsigned int>(time(0)));
	num1 = rand() % 200 + 800;
	num2 = 1 + rand() % 200;
	
	do
	{
		//Display the menu to user and get user input
		cout << "Select one of the following options" << endl;
		cout << "-----------------------------------------" << endl;
		cout << "1. [A]dd         [+]" << endl;
		cout << "2. [S]ubtract    [-]" << endl;
		cout << "3. [M]ultiply    [x]" << endl;
		cout << "4. [D]ivide      [/]" << endl;
		cout << "5. [Q]uit           " << endl;
		cin >> choice;
		cout << "Your choice is: " << choice << endl;


		//Respond to user's selection
		switch (choice)
			{
			
			case 'A':
			case 'a':
			case '1':
			case '+':
				answer = num1 + num2;
				cout <<""<< num1 << endl;
				cout << "+" << num2 << endl;	
				cout << "-----" << endl;
				cout << "Enter your answer: " << useranswer;
				if (answer == useranswer)
					{
					cout << "Congratulation, Your answer is correct" << endl;
					cout << num1;
					cout << "+" << num2;
					cout << "------" << endl;
					cout << answer;

					}
				else
					{
					cout << "You answer is not correct" << endl;
					cout << "The answer is: " << answer;
					}
				break;
			case 'S':
			case 's':
			case '2':
			case '-':
				answer = num1 - num2;
				cout << num1;
				cout << "-" << num2;
				cout << "------" << endl;
				cout << "Enter your answer: " << useranswer;
				if (answer == useranswer)
					{
					cout << "Congratulation, Your answer is correct" << endl;
					cout << num1;
					cout << "-" << num2;
					cout << "------" << endl;
					cout << answer;
					}
				else
					{
					cout << "You answer is not correct" << endl;
					cout << "The answer is: " << answer;
					}
				break;

			//Seed the second number again
			num2 = 1 + rand() % 10;

			case 'M':
			case 'm':
			case '3':
			case 'x':
				answer = num1 * num2;
				cout << num1;
				cout << "x" << num2;
				cout << "------" << endl;
				cout << "Enter your answer: " << useranswer;
				if (answer == useranswer)
					{
					cout << "Congratulation, Your answer is correct" << endl;
					cout << num1;
					cout << "x" << num2;
					cout << "------" << endl;
					cout << answer;
					}
				else
				{
					cout << "You answer is not correct" << endl;
					cout << "The answer is: " << answer;
				}
				break;

		
			
			case 'D':
			case 'd':
			case '4':
			case '/':
				answer = (num1/num2);
				cout << "        __________" << endl;
				cout << num2 << ")" << num1 << endl;
				cout << "Enter your answer, both the quotient";
				cout << "and the remainder separated by a space: " << endl;
				cin >> useranswer >> remainder;
				if ((useranswer == answer) && (userremainder = remainder))
					{
					cout << "Congratulation, Your answer is correct" << endl;
					cout << "        __________" << endl;
					cout << num2 << ")" << num1 << endl;
					cout << "The answer is: " << answer << "and the remainder is: " << remainder << endl;
					}
				else 
				{
					cout << "Sorry. The correct answer is: " << answer << " and the remainder is: " << remainder << endl;

				}
			case 'Q':
			case 'q':
			case '5':
				cout << "Exiting the program....." << endl;
				break;

			//Validate user's chocie
			default:
				{
					cout << "Please enter a valid choice" << endl;
					cout << "You can enter a number from 1 to 5 or" << endl;
					cout << "A letter corresponding to the calculation that you want to do" << endl;
					cin >> choice;
					cout << "Your choice is: " << choice;
				}


			}

		//Pause the screen
			if (choice != 5)
			{
				cout << endl;
				cin.ignore(80, '\n');
				cout << "Hit the enter key to continue....\n";
				cin.get();
				cout << endl;
			}


	} while (choice != 5);

	return 0;
}


I can compile and run the program but when I enter A or a or 1 or +, it won't do the computation.

Thank you for reading.
Last edited on
closed account (48T7M4Gy)
Your while is selecting on the basis of the numbers 1 and 5 and not the characters '1' and '5'. You also need to check for the other character input possibilities. Quite a long list of or's
Your while is selecting on the basis of the numbers 1 and 5 and not the characters '1' and '5'. You also need to check for the other character input possibilities. Quite a long list of or's


well what I want is if the user select A or a or 1 or +, the addition computation will happen. if it's anything else, the message error will show up. How do I do this ? Is the do-while loop a bad choice ? Should I use for loop instead ?
Last edited on
closed account (48T7M4Gy)
I understood what you are trying to do and there is nothing wrong in principle with giving many options for the same type of calculation. All it does is make a very long test for validating the input. Keep it simple and you don't have such a long list. But there's nothing wrong and there's probably no better way of doing it. You could save a little by using toupper() to get everything into uppercase before the test but that's marginal.

FWIW I would only have 1 option in each case - 1, 2, 3 or 4 but there you go.

There's also nothing wrong with using a while loop. It's better than lots of if's, else's etc.
:-)
Last edited on
use "default" to force the loop back to the menu. If the user has entered a valid choice it will never get to default, so it becomes the fallback option for any incorrect entries.
Thank you kemort and PCrumley.

I follow PCrumley instruction and got rid of that problem. Now i can compile the code but it keep pop up
run-time check failure #3 the variable answer is being used without being initialized.

I updated my code on 1st post
closed account (48T7M4Gy)
I can't get it toreproduce that error on my machine but what it means is you might like to change line 28:

int num1, num2, answer = 0, useranswer = 0, remainder, userremainder;
Last edited on
Thank you kemort for helping me. All problems are solved.
Last edited on
Topic archived. No new replies allowed.