Math Game c++ , division issue.

Hello

Got a small problem here , I spent 2 days writing this with debugging sessions , alone, here is the thing its working and all but sometimes it just divide by 0 , I tried adding a
while(val2=0)
val2=//my random number generator.
but no luck .
and another thing is that what ever I do I cant restart the score after each stage completed.
for example ,
//operation enetered.
//diificulty stage chosen.
// first stage have score of more than 60 , pass to the next stage.
//next stage score should be 100 if u answer all questions , instead its 100 + prev score .






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
 #include<iostream> 
#include<cstdlib> 
#include<time.h>
#include<math.h>
using namespace std;
// I will put diffrent functions _one for each operation_ here.
int main()
{
	int dif;
	cout << "Please choose the operation you want to practice today (+,-), or(/,*): ";
	char op;
	cin >> op;
	if (op != ('-') && op != ('+') && op != ('*') && op != ('/'))//if not a valid operator
	{
		cout << "Invalid operator" << endl;
		system("pasue");
		exit(0);
	}
	cout << "Please enter the level of difficult (1 and up): ";
	cin >> dif;
	srand(time(0));
	int min, max;//generate two random numbers in the range min and max of digit size as dif
	int numr = 0;
	int numw = 0;
	int i;
	int val1, val2;
	int res, ans;
	double score;
	bool correct;
	char con;
	int chances;
	do {
		for (i = 0; i < 5; i++)//5 operations
		{
			chances = 3;
			min = int(pow(10, dif -1));
			max = int(pow(10, dif) - 1);
			val1 = min + rand() % ((max + 1) - min);
			val2 = min + rand() % ((max + 1) - min);
			if (op == '/')//for division operator if second value is zero then repeatedly generate second number until it is not a zero
			{

			
				while (val2 != 0)
					val2 = min + rand() % ((max + 1) - min)+1;

	


			}

			do {
				//res stores user's answer, ans has correct answer for(j=0:j<3.j++)
				cout << "what is " << val1 << " " << op << " " << val2 << ": ";
				if (op == '*')

					ans = val1 * val2;

				else if (op == '/')

					ans = val1 / val2;

				else if (op == '+')

					ans = val1 + val2;

				else if (op == '-')

					ans = val1 - val2;

				cin >> res;
				if (res == ans)
				{
					cout << "correct answer" << endl;
					numr++;
					correct = true;
				}
				else
				{
					cout << "Incorrect!";
					numw++;
					correct = false;
					chances--;
				}
			} while (!correct && chances != 0);
		}

		score = ((double)numr / (numr + numw)) * 100;
		cout << "score: " << score << "%" << endl;//no of correct/total
		cout << "Continue? [Y/N]" << endl;
		cin >> con;
		if (con == 'N'||con=='n')
		{
			system("pause");// considering break;
			return 0;
		}
	
		if (score >= 80)
		{
			dif++;
			score = 0;
		}
		else if (score >= 60)
		{
			dif++;
			score = 0;
		}
		else
		{
			cout << "try again" << endl;
			
		}
		score = 0;
	} while (con == 'Y'||con=='y');
	
	system("pause");
	return 0;
}
1
2
				while (val2 != 0)
					val2 = min + rand() % ((max + 1) - min)+1;

loops endles. How about while (val2 == 0)?
remove the while. min is based off difficulty which appears to be positive (not enforced but requested in the text).

as written, the unchanged logic (which is probably wrong) would be
val2 = 0;
as probably intended, the code would be
if(!val2) val2 = min + rand() % ((max + 1) - min)+1;

the while does the same as the if, but intent is muddled reading it.
Last edited on
1
2
if(!val2)
	val2 = min + rand() % ((max + 1) - min) + 1;


I did this and it works , tried it for 20 times and it never divides by 0.
is there a way I can shorten my double variable ?
because when it gives me something like 1/9 and I give it 0.11 it never gets it even if I put the full number which is 0.1111111111 .
here is the output .

Please choose the operation you want to practice today (+,-), or(/,*): /
Please enter the level of difficult (1 and up): 1
what is 6 / 8: 0.75
Incorrect!what is 6 / 8:


same thing happened with me with ( 1/9 ).
I completed all the other operations and they work perfectly , the stages too.
what is 6 / 8:

It's 0. Either (i) learn about integer division; or (ii) use doubles.
it still won't work.

minor: its <ctime> and <cmath> you are using C language header files. They work, until they don't, it can cause problems.

major:
as lastchance said you need to understand how the computer does integeer math.
first, 6/8 is smaller than 1, so it is truncated down to zero. 100/11 is 9, for the same reasons.

second, even if you change to doubles, your == is going to give trouble. some numbers cannot be represented exactly as doubles, so even if the human types in the correct answer, it will not match. I haven't memorized specific examples, you can find plenty on google, but it "like" if you computed 6/8 and the actual answer ways 0.7499999999999999998 but you typed 0.75 and it changed that to 0.7500000000000000000001 instead. Again, made up the numbers, but they are not equal, just very close ...

so you need to switch to doubles and then handle matching that with the user's input. a simple way to do that is simply to see if abs(userinput - calculated result) <= 0.001 (or whatever precision you expect).




Last edited on
Hey guys thank you for your support , I did learn new stuff and yes I was focused on my logical prespective so much I forgot about the math one.
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include<iostream> 
#include<cstdlib> 
#include<time.h>
#include<math.h>
#include<ctime>
#include<typeinfo>
#include<string>
#include<windows.h>
#include<wchar.h>
#include<stdio.h>
#include<MMsystem.h>
#include <chrono>

using namespace std;
void Intro()
{
	PlaySound(TEXT("hello-4"), NULL, SND_SYNC);
	cout << endl;
	cout << "Welcome to the Mathematics Practice Software.\n" << endl;
	cout << endl;
	cout << "This program will help you practice math (+,-,*,/) by ";
	cout << "giving you with randomly generated questions,\n";
	cout << "problems are at different level of difficulty.\nThe ";
	cout << "difficulty is determined by the number of digits in ";
	cout << "the question.\nYou can specify the initial ";
	cout << "number of digits. The program will also keep track ";
	cout << "of your progress.\nAt the end of each session, you ";
	cout << "may choose to continue or quit." << endl;
	cout << endl;
	cout << "Please choose the operation you want to practice today (+,-, /,or *): ";

}
void gen_numbers(int &min, int &max, int &val1, int &val2, int  &dif)
{
	min = int(pow(10, dif - 1));
	max = int(pow(10, dif) - 1);
	val1 = min + rand() % ((max + 1) - min);
	val2 = min + rand() % ((max + 1) - min);
}
void ask_questions(bool &correct, int &chances, int &numr, int &numw, long double res, long double ans, char op)
{
	if (cin.fail())
	{
	
	cout << "Error\n";
	system("pause");
	exit(0);
	}
	else
	{
		if (op == '/')
		{
			ans = ans * 100;
			res = res * 100;
			ans = ceil(ans);
			if (res >= ans - 1 || res <= ans + 1)
			{
				cout << "correct answer" << endl;
				numr++;
				correct = true;
				PlaySound(TEXT("app.wav"), NULL, SND_SYNC);
			}
			else
			{
				cout << "Incorrect!";
				numw++;
				correct = false;
				chances--;
				PlaySound(TEXT("sad.wav"), NULL, SND_SYNC);
			}
		}
		else if (op != '/')
			if (res == ans)
			{
				cout << "correct answer" << endl;
				numr++;
				correct = true;
				PlaySound(TEXT("app.wav"), NULL, SND_SYNC);
			}

			else
			{
				cout << "Incorrect!";
				numw++;
				correct = false;
				chances--;
				PlaySound(TEXT("sad.wav"), NULL, SND_SYNC);
			}
	}
}

int main()
{

	srand(time(0));
	SYSTEMTIME time;
	while (true)
	{
		GetLocalTime(&time);
		wprintf(L"The local Time: %02d:%02d:%02d\n", time.wHour, time.wMinute, time.wSecond);
		int dif;
		Intro();
		char op;
		cin >> op;
		if (op != ('-') && op != ('+') && op != ('*') && op != ('/'))//if not a valid operator
		{
			cout << "Invalid operator" << endl;
			system("pause");
			exit(0);
			
		}
		cout << "each increment in difficulty increments the number of digits inside each part of the question.\n";
		cout << "Please enter the level of difficult (1 and up): ";
		cin >> dif;
		
		int min, max;//generate two random numbers in the range min and max of digit size as dif

		int i;
		int val1, val2;
		double res;
		double ans;
		double score;
		bool correct;
		char  con;
		int chances;
		do {
			int numr = 0;
			int numw = 0;

			for (i = 0; i < 5; i++)//5 operations
			{



				chances = 3;
				gen_numbers(min, max, val1, val2, dif);
				if (op == '/')//for division operator if second value is zero then repeatedly generate 
					//second number until it is not a zero
				{


					if (!val2)
						val2 = min + rand() % ((max + 1) - min) + 1;




				}
				if (val1 == 0 || val2 == 0)
				{
					cout << "error\n";
					system("pause");
					exit(0);
				}
				if (dif > 10)
				{
					cout << "our data types ant take more than 10 digits per value.\n";
					system("pause");
					exit(0);
				}

				do {
					ans = 0;
					//res stores user's answer, ans has correct answer for(j=0:j<3.j++)
					cout << "what is " << val1 << " " << op << " " << val2 << ": ";
					if (op == '*')

						ans = val1 * val2;

					else if (op == '/')
					{

						//divisionproblem(ans, val1, val2, dif);
						ans = val1 / double(val2);
						ans = ceil(ans * 100) / 100;
					}
					else if (op == '+')

						ans = val1 + val2;


					else if (op == '-')

						ans = val1 - val2;

					cin >> res;

					ask_questions(correct, chances, numr, numw, res, ans, op);
				} while (!correct && chances != 0);

			}

			score = ((double)numr / (numr + numw)) * 100;
			cout << "score: " << score << "%" << endl;//no of correct/total
			//callme(score);
			if (score >= 80)
			{
				
				cout << "Exelent\n";
			}
			else if (score >= 60)
			{
				
				cout << "Work Harder!\n";
			}
			else
			{
				cout << "try again" << endl;
				cout << "you Didnot Pass\n";
			}
			cout << "Continue? [Y/N]" << endl;
			cin >> con;
			if (con == 'N' || con == 'n')
			{
				GetLocalTime(&time);
				wprintf(L"The local Time: %02d:%02d:%02d\n", time.wHour, time.wMinute, time.wSecond);
				PlaySound(TEXT("song.wav"), NULL, SND_SYNC);
				system("pause");// considering break;

				break;

			}

			if (score >= 80)
			{
				dif++;

			}
			else if (score >= 60)
			{
				dif++;

			}
			else
				cout << endl;

			cout << "next stage\n ";
		} while (con == 'Y' || con == 'y');

		system("pause");
		return 0;
	}
}

here is the final code , its working for a maximum of 10 digits per value in each question , and I used a function to play sounds with .wav format .
this program got me to understand c++ more .
@jonnin I did a different thing you can see it in the functions above I multiplied by 100 and ceiled them then compared and if they are different with a certan range then its true else its wrong.
1
2
3
4
5
6
7
8
9
10
if (op == '/')
		{
			ans = ans * 100;
			res = res * 100;
			ans = ceil(ans);
			if (res >= ans - 1 || res <= ans + 1)
			{
				cout << "correct answer" << endl;
				numr++;
				correct = true;
Topic archived. No new replies allowed.