if else statement

I am trying to create a program that will generate 22 questions math problems.
1 number will range from 0-20 the 2nd number will range from 1 to 20.
the math functions performed are +,-,*./,%
All should be randomly generated.
Name at the beginning.
Let the user know when an answer is wrong and what the correct answer is.(only for wrong)
give a perctage grade at the end

I have a lot of it. I am stuck at my if else statements.
If the statement is correct I want it to just print "correct"
else if wrong say its wrong and give the correct answer

I have also made some notes in the program at that point. I am not sure where I am using a switch how to do this. I need some help.

#include <cstdlib>
#include <ctime>
#include<iostream>
using std::cin;
using std::cout;


int main()
{
srand(time(0));

const int SPACES=100;
char Name[SPACES]={};
cout<< "Please enter your name: ";
cin>> Name;

const int PLUS=0;
const int MINUS =1;
const int DIVIDE=2;
const int MULTIPLY=3;
const int MOD=4;
int a,b,i,op;
int answer=0, userGuess;
int total=0;
float myfloat=1.00, percentage=0;

for(int i=1 ; i<=22; i++)
{
a = rand() % 21; // range 0-20
b = (rand() % 20) + 1; //range 1-20
op = rand() % 5; //range 0-4 to match a operation

switch (op)
{
case PLUS:
answer = a + b;
cout << "\n\nWhat is " << a << " + " << b << "? ";
cin >> userGuess;
break;

case MINUS:
answer = a - b;
cout << "\n\nWhat is " << a << " - " << b << "? ";
cin >> userGuess;
break;

case DIVIDE:
answer = a / b;
cout << "\n\nWhat is " << a << " / " << b << "? ";
cin >> userGuess;
break;

case MULTIPLY:
answer = a * b;
cout << "\n\nWhat is " << a << " * " << b << "? ";
cin >> userGuess;
break;

case MOD:
answer = a % b;
cout << "\n\nWhat is " << a << " % " << b << "? ";
cin >> userGuess;

// there is something wrong here. If it was 1 problem
// i would put somthing like: if(userGuess==a+b)
// if I enter a correct or wrong number sometime it works sometimes it doesn't
// I don't know how to fix it.


if(answer==userGuess)
{
cout << "you are correct." << "\n";
++answer;
}
else
{
cout<< " \n\nThats the wrong answer"<< " "<< "\n";
++total;
}
}
}

percentage = 100* (answer / (total/myfloat) );

cout << "You got " << percentage << "% right.\n";

return 0;
}
I TRIED to make code more readable by using tabs (and CODE TAGS; see that image on right-> it has looks like "<>" ). Can you spot errors more easily? look close enough for brackets (as a 1 thing)

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
#include <cstdlib>
#include <ctime>
#include<iostream>
using std::cin;
using std::cout;


int main()
{
	srand(time(0));

	const int SPACES=100;
	char Name[SPACES]={};
	cout<< "Please enter your name: ";
	cin>> Name;

	const int PLUS=0;
	const int MINUS =1;
	const int DIVIDE=2;
	const int MULTIPLY=3;
	const int MOD=4;
	int a,b,i,op;
	int answer=0, userGuess;
	int total=0;
	float myfloat=1.00, percentage=0;

	for(int i=1 ; i<=22; i++)
	{
		a = rand() % 21; // range 0-20
		b = (rand() % 20) + 1; //range 1-20
		op = rand() % 5; //range 0-4 to match a operation

		// Where switch block ends? duh....
		switch (op)
		{
			case PLUS:
				answer = a + b;
				cout << "\n\nWhat is " << a << " + " << b << "? ";
				cin >> userGuess;
				break;

			case MINUS:
				answer = a - b;
				cout << "\n\nWhat is " << a << " - " << b << "? ";
				cin >> userGuess;
				break;

			case DIVIDE:
				answer = a / b;
				cout << "\n\nWhat is " << a << " / " << b << "? ";
				cin >> userGuess;
				break;

			case MULTIPLY:
				answer = a * b;
				cout << "\n\nWhat is " << a << " * " << b << "? ";
				cin >> userGuess;
				break;

			case MOD:
				answer = a % b;
				cout << "\n\nWhat is " << a << " % " << b << "? ";
				cin >> userGuess;



		if(answer==userGuess)
		{
			cout << "you are correct." << "\n";
			++answer;
		}
		else
		{
			cout<< " \n\nThats the wrong answer"<< " "<< "\n";
			++total;
		}
	}
}

percentage = 100* (answer / (total/myfloat) );

cout << "You got " << percentage << "% right.\n";

return 0;
}
Last edited on
Sorry, I tried to do that but it did carry over when it copied. My appoligies.

I thought all my brackets where correct.

line 77 is for the end of my switch statement
line 78 is for the end of my for statement
if that is wrong that maybe that is my mistake, but that was my thinking
Last edited on
So; can you see even 1 error? ;) answers are buried in the post i give to you.
sorry, i guess i need to read better. That fixed it. thanks

Last edited on
There's a misplaced bracket, isn't there?

Are you closing your switch statement correctly???
Topic archived. No new replies allowed.