What's wrong with my code?

Write your question here.
My code was to have a loop that asked 5 math question; ignoring division.
It keeps telling me my error is on my totalsum=num1*num2
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
  #include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
int main()
{
int num1, num2, operation, answer, totalsum, correct, average, count;
srand(time(0));
num1 = rand()%11;
num2 = rand()%11;
operation = rand()%3;
average = 0;
correct = 0;
for ( count = 0; count < 5; count++){
if (operation = 0)
	{
	cout << num1 << " + " << num2 << endl;
	cin >> answer;
	totalsum = num1+num2;
	if (answer == totalsum)
		correct ++;
		}
else if (operation = 1)
	{
	cout << num1 << " - " << num2 << endl;
	cin >> answer;
	totalsum = num1-num2;
	if (answer == totalsum)
		correct ++;
		}
else
	{
	cout << num1 << " * " << num2 << endl;
	cin >> answer
	totalsum = num1*num2
	if (answer == totalsum)
		correct ++;
	}
}
average = correct/count;
cout << "Your average is " << average << endl; 
return 0;
}
cin >> answer;
totalsum = num1*num2;
You forgot semicolons for those statements.
Last edited on
Thx but now when i run it, it only shows the same problems 5 times.
Can you please paste the errors you are getting?
There's no error but it keeps showing the same question again. For example, the program asked 8+2. I answer 10. Then the same problem occurs again and I answer it again. It keeps on having the same questions until I answer the question 5 times
Thats because once you do this to operation - operation = rand() % 3;

And lets say, it randomly gets the number 2. Then you enter the loop and it never changes from number 2. Same goes with num1 = rand() % 11; and num2 = rand() % 11;

Put these -

1
2
3
num1 = rand() % 11;
		num2 = rand() % 11;
		operation = rand() % 3;


Inside your for-loop -

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
int num1, num2, operation, answer, totalsum, correct, average, count;
	srand(time(0));

	average = 0;
	correct = 0;

	for (count = 0; count < 5; count++){

		num1 = rand() % 11;
		num2 = rand() % 11;
		operation = rand() % 3;
		if (operation = 0)
		{
			cout << num1 << " + " << num2 << endl;
			cin >> answer;
			totalsum = num1 + num2;
			if (answer == totalsum)
				correct++;
		}
		else if (operation = 1)
		{
			cout << num1 << " - " << num2 << endl;
			cin >> answer;
			totalsum = num1 - num2;
			if (answer == totalsum)
				correct++;
		}
		else
		{
			cout << num1 << " * " << num2 << endl;
			cin >> answer;
				totalsum = num1*num2;
				if (answer == totalsum)
					correct++;
		}
	}
	average = correct / count;
	cout << "Your average is " << average << endl;
the variable that holds the random generation for operation is in outside the loop,
put it inside
No need To Copy Paste What I said a comment above you @Lorence30
Another problem with your code is that lines 16 and 24 use the assignment operator (=), not the comparison operator (==).
Topic archived. No new replies allowed.