Math Tutor

Hi I've been having issues trying to get the choice 5 to function properly. This program is supposed to function as a math tutor and after each choice input (other than 5), after solving the problem, the user should be brought to the main choices once again. Currently what I have forces the program to shut down after the first answer is input. Can someone tell me if I'm missing something?

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
187
188
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main () {

	int i;
	for ( ; ; ) {

	cout << "      Math Tutor Menu:      " << endl;
	cout << "----------------------------" << endl;
	cout << "1. Addition problem" << endl;
	cout << "2. Subtraction problem" << endl;
	cout << "3. Multiplication problem" << endl;
	cout << "4. Division problem" << endl;
	cout << "5. Quit this program" << endl;
	cout << "----------------------------" << endl;
	cout << "Enter your choice (1-5): ";
	int choice;
	cin >> choice;
	if (choice < 1) {
		cout << endl;
		cout << "Please enter in a number (1-5) that corresponds with the choice that you want on the menu.";
		cout << endl;
	}
	if (choice > 5) {
		cout << endl;
		cout << "Please enter in a number (1-5) that corresponds with the choice that you want on the menu.";
		cout << endl;
	}
	cout << endl;

	if (choice == 1){
		srand(time(0));
		int aadd;
		int badd;
		int maxValueadd = 500;
		int minValueadd = 50;
		aadd = (rand() % (maxValueadd - minValueadd + 1)) + minValueadd;
		badd = (rand() % (maxValueadd - minValueadd + 1)) + minValueadd;
		if ( aadd < 100 )
			cout << "   " << aadd << endl;
		if ( aadd > 100 )
			cout << "  " << aadd << endl;
		if ( badd < 100 )
			cout << "+  " << badd << endl;
		if ( badd > 100 )
			cout << "+ " << badd << endl;
		cout << "------" << endl;
		int inputadd;
		cout << "  ";
		cin >> inputadd;
		int cadd;
		cadd = aadd + badd;
		if ( inputadd == cadd )
		{
			cout << endl;
			cout << "Congratulations, that is correct!" << endl;
			cout << "The answer is " << cadd << "." << endl;
			cout << endl;
		}
		if ( inputadd != cadd )
		{
			cout << endl;
			cout << "Sorry, that is incorrect!" << endl;
			cout << "The answer is " << cadd << "." << endl;
			cout << endl;
		}
	}

	if (choice == 2) {
		srand(time(0));
		int asub;
		int bsub;
		int maxValuesub = 500;
		int minValuesub = 50;
		asub = (rand() % (maxValuesub - minValuesub + 1)) + minValuesub;
		bsub = (rand() % (maxValuesub - minValuesub + 1)) + minValuesub;
		if ( asub < 100 )
			cout << "   " << asub << endl;
		if ( asub > 100 )
			cout << "  " << asub << endl;
		if ( bsub < 100 )
			cout << "-  " << bsub << endl;
		if ( bsub > 100 )
			cout << "- " << bsub << endl;
		cout << "------" << endl;
		int inputsub;
		cout << "  ";
		cin >> inputsub;
		int csub;
		csub = asub - bsub;
		if ( inputsub == csub )
		{
			cout << endl;
			cout << "Congratulations, that is correct!" << endl;
			cout << "The answer is " << csub << "." << endl;
			cout << endl;
		}
		if ( inputsub != csub )
		{
			cout << endl;
			cout << "Sorry, that is incorrect!" << endl;
			cout << "The answer is " << csub << "." << endl;
			cout << endl;
		}
	}

	if (choice == 3) {
		srand(time(0));
		int amul;
		int bmul;
		int maxValueamul = 100;
		int minValueamul = 1;
		int maxValuebmul = 9;
		int minValuebmul = 1;
		amul = (rand() % (maxValueamul - minValueamul + 1)) + minValueamul;
		bmul = (rand() % (maxValuebmul - minValuebmul + 1)) + minValuebmul;
		if ( amul < 100 )
			cout << "   " << amul << endl;
		if ( amul > 100 )
			cout << "  " << amul << endl;
		if ( amul == 100)
			cout << " " << amul << endl;
		cout << "x   " << bmul << endl;
		cout << "------" << endl;
		int inputmul;
		cout << " ";
		cin >> inputmul;
		int cmul;
		cmul = amul * bmul;
		if ( inputmul == cmul )
		{
			cout << endl;
			cout << "Congratulations, that is correct!" << endl;
			cout << "The answer is " << cmul << "." << endl;
			cout << endl;
		}
		if ( inputmul != cmul )
		{
			cout << endl;
			cout << "Sorry, that is incorrect!" << endl;
			cout << "The answer is " << cmul << "." << endl;
			cout << endl;
		}
	}

	if (choice == 4) {
		srand(time(0));
		int adiv;
		int bdiv;
		int maxValueadiv = 50;
		int minValueadiv = 1;
		int maxValuebdiv = 9;
		int minValuebdiv = 1;
		adiv = (rand() % (maxValueadiv - minValueadiv + 1)) + minValueadiv;
		bdiv = (rand() % (maxValuebdiv - minValuebdiv + 1)) + minValuebdiv;
		int cdiv;
		cdiv = adiv * bdiv;
		cout << cdiv << " / " << bdiv << " = ";
		int inputdiv;
		cin >> inputdiv;
		if ( inputdiv == adiv )
		{
			cout << endl;
			cout << "Congratulations, that is correct!" << endl;
			cout << "The answer is " << adiv << "." << endl;
			cout << endl;
		}
		if ( inputdiv != adiv )
		{
			cout << endl;
			cout << "Sorry, that is incorrect!" << endl;
			cout << "The answer is " << adiv << "." << endl;
			cout << endl;
		}
	}

	if (choice == 5){
		cout << "Thank you for using Math Tutor." << endl;
		cout << endl;
}
		return 0;
	}

}
I think you have your return statement (line 184) in the wrong place. Try putting it a couple of lines below, on 186, just above the final brace.
Hello ajohns2,

In place of the endless for loop it would be more appropriate to use while (1).

Line 184. The return statement is in the wrong place as Cheddar99 has pointed out.

In its place put "break;" to exit the while loop.

That is a start until I have a chance to test the rest of the program. Which I will do directly. I will let you know what I find.

Hope that helps,

Andy

Edit: Forgot to mention "srand()" only need to be done once. I would put it before the for loop.
Last edited on
Hello ajohns2,

After testing the program it appears to work with out any problems.

Something you might want to try is:
1
2
std::cout << std::setw(5) << aadd << '\n';
std::cout << "+ " << std::setw(3) << badd << '\n';

These two lines can replace:
1
2
3
4
5
6
7
8
if (aadd < 100)
	cout << "   " << aadd << endl;
if (aadd > 100)
	cout << "  " << aadd << endl;
if (badd < 100)
	cout << "+  " << badd << endl;
if (badd > 100)
	cout << "+ " << badd << endl;

What you have done to input the answer works.

For the if statements you do not need to check both ways. This will work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (inputadd == cadd)
{
	cout << endl;
	cout << "Congratulations, that is correct!" << endl;
	cout << "The answer is " << cadd << "." << endl;
	cout << endl;
}
else
{
	cout << endl;
	cout << "Sorry, that is incorrect!" << endl;
	cout << "The answer is " << cadd << "." << endl;
	cout << endl;
}


The only concern I had was a divide by zero, but if I understand random number generation correctly the smallest number it will return is one.

After the menu the two if statements can be shortened to:
1
2
3
4
5
6
if (choice < 1 || choice > 5)
{
	cout << endl;
	cout << "Please enter in a number (1-5) that corresponds with the choice that you want on the menu.";
	cout << endl;
}


Hope that helps,

Andy
Topic archived. No new replies allowed.