program checks user's math answer, WON'T WORK! PLEASE HELP!

Hi there! my code is supposed to ask the user what math operation they want to do, ask their answer, an d check if it is correct. It gives 10 points for the first try, 5 for the second, and 2 for the third. It should stop the program after three tries. The problem is that it continuously asks what the user's answer is but never corrects it or displays the point values. Please help! This is due today and the marking period ends in a few days, I really need to pass this class!

(it also uses a library, which I have attached at the bottom)

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
//this is the program
#include <iostream>
#include "easymathlib.h"
#include <cstdlib>
using namespace std;
int main() 
{	
	int response;
	int answer;
	int UserAnswer;
	int points=0;
	int i=0;
	int Number1;
	int Number2;
	
	cout << "Enter the number for the problem type desired:\n\t 1) Addition\n\t 2) Subtraction\n\t 3) Multiplication\n\n Enter Choice: ";
		cin >> response;
	Number1 = rand() % 100+1;    // wheel 1 in range 1 to 100		
	Number2 = rand() % 100+1;    // wheel 1 in range 1 to 100
	
		for (i=0; i<=3; i++)
			{
				for (i=1; i<=3; i++)
				{
				
				while (response==1)
					{
						cout << "The first number is: ";
							cout << Number1;
						cout << "The second number is: ";
							cout << Number2;
						add(Number1, Number2);
						cout << "What is your answer? ";
							cin >> UserAnswer;
						
						if (UserAnswer=answer && i==1)
							{
								points=points+10;
								cout << "Your points: ";
									cout << points;
							}	
						else if (UserAnswer=answer && i==2)
							{
								points=points+5;
								cout << "Your points: ";
									cout << points;
							}	
						else if (UserAnswer=answer && i==3)
							{
								points=points+2;
								cout << "Your points: ";
									cout << points;
							} 
						if (UserAnswer!=answer && i==1)
						{
							cout << "WRONG! TRY AGAIN.\n";
							cout << "Your points: ";
							cout << points;
						}
						else if (UserAnswer!=answer && i==2)
						{
							cout << "WRONG! LAST TRY.\n";
							cout << "Your points: ";
							cout << points;
						}
						else if (UserAnswer!=answer && i==3)
						{
						    cout << "WRONG! The answer is "<<answer<<endl;
						    cout << "Your points: ";
							cout << points;
						}
						
					}
				while (response==2)
					{
						cout << "The first number is: ";
							cout << Number1;
						cout << "The second number is: ";
							cout << Number2;
						subtract(Number1, Number2);
						cout << "What is your answer? ";
							cin >> UserAnswer;
						
						if (UserAnswer=answer && i==1)
							{
								points=points+10;
								cout << "Your points: ";
									cout << points;
							}	
						else if (UserAnswer=answer && i==2)
							{
								points=points+5;
								cout << "Your points: ";
									cout << points;
							}	
						else if (UserAnswer=answer && i==3)
							{
								points=points+2;
								cout << "Your points: ";
									cout << points;
							}
						if (UserAnswer!=answer && i==1)
						{
							cout << "WRONG! TRY AGAIN.\n";
							cout << "Your points: ";
							cout << points;
						}
						else if (UserAnswer!=answer && i==2)
						{
							cout << "WRONG! LAST TRY.\n";
							cout << "Your points: ";
							cout << points;
						}
						else if (UserAnswer!=answer && i==3)
						{
						    cout << "WRONG! The answer is "<<answer<<endl;
						    cout << "Your points: ";
							cout << points;
						}
						
					}
				while (response==3)
					{
					    cout << "The first number is: ";
							cout << Number1;
						cout << "The second number is: ";
							cout << Number2;
						multiply(Number1, Number2);
						cout << "What is your answer? ";
							cin >> UserAnswer;
						
						if (UserAnswer=answer && i==1)
							{
								points=points+10;
								cout << "Your points: ";
									cout << points;
							}	
						else if (UserAnswer=answer && i==2 )
							{
								points=points+5;
								cout << "Your points: ";
									cout << points;
							}	
						else if (UserAnswer=answer && i==3 )
							{
								points=points+2;
								cout << "Your points: ";
									cout << points;
							}
						if (UserAnswer!=answer && i==1)
						{
							cout << "WRONG! TRY AGAIN.\n";
							cout << "Your points: ";
							cout << points;
						}
						else if (UserAnswer!=answer && i==2)//and statement
						{
							cout << "WRONG! LAST TRY.\n";
							cout << "Your points: ";
							cout << points;
						}
						else if (UserAnswer!=answer && i==3)
						{
						    cout << "WRONG! The answer is "<<answer<<endl;
						    cout << "Your points: ";
							cout << points;
						}
						
					}
				}
			}
}


------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//this is the library
#include <iostream>
using namespace std;
//add function
void add(int x, int y)
    {
        int answer;
        answer=x+y;
    }
    
//subtract function
void subtract(int x, int y)
    {
        int answer;
        answer=x-y;
    }
//multiply  function
void multiply(int x, int y)
{
        int answer;
        answer=x*y;
    }
Hello cppnewbie13,
I believe I see every thing except "easymathlib.h". Without this I can not test the program.

If the second bit of code is the "easymathlib.h" it should not be.

I can look t the code as is, but it will take less time to see how the program run and what variables contain as the program runs.

Andy
Well just to start something rolling, two obvious issues I see is that on lines 36, 42, 48, 84, 90, 96, 132, 138, 144, you are using the '=' (assignment) operator to try to test for equality instead of the '==' (equality) operator.

e.g. apple == dog is checking for equality, but apple = dog is assigning apple the value of dog.

I'm also not sure what you are trying to do with your add, subtract, and multiply functionality. Your add, subtract, and multiply functions don't return anything; they are effectively useless in their current form. If you want to use the value, you should return it.
1
2
3
4
5
6
//add function
int add(int x, int y)
{
    int answer=x+y;
    return answer;
} 


The way you set up your for loop is also very weird and error prone.

1
2
3
4
5
6
7
for (i=0; i<=3; i++)
{
    for (i=1; i<=3; i++)
    {
        // ...
    }
}

In your inner for loop, you are simply overwriting the value of i that's in the outer for loop. If you want two independent loop variables, you should give different variable names. But I don't think you need 2x for loops here -- one should be enough for your logic.

You should also consider keeping a variable inside only the smallest scope necessary for that variable. So, instead of defining int i at the beginning of your main, define it within the for loop itself.
1
2
3
4
for (int i = 0; i < 3; i++) // loop will run 3 times, not 4
{
    // "i" is only now defined within the loop
}


You also have a lot of repetition in logic; I would suggest finding a way to factor out the similar logic into a function. But this isn't an error in itself.

___________________________________________________
___________________________________________________

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
//this is the program
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void display_numbers(int n1, int n2)
{
	cout << "The first number is: ";
	cout << n1 << endl;
	cout << "The second number is: ";
	cout << n2 << endl;
}

void ask_question(int n1, int n2, char operator_symbol)
{
	cout << "What is " << n1 << " " << operator_symbol << " " << n2 << "? ";
}

int main() 
{
	srand(time(nullptr)); // initial random seed based on current system's time
	
	int points = 0;

	// ask the user what math operation they want to do:
	int response;
	cout << "Enter the number for the problem type desired:\n\t 1) Addition\n\t 2) Subtraction\n\t 3) Multiplication\n\n Enter Choice: ";
	cin >> response;
	int Number1 = rand()%100 + 1; // wheel 1 in range 1 to 100		
	int Number2 = rand()%100 + 1; // wheel 1 in range 1 to 100
	
	display_numbers(Number1, Number2);
	
	for (int i = 1; i <= 3; i++)
	{
		int correct_answer;
		if (response == 1) // Addition
		{
			correct_answer = Number1 + Number2;
			ask_question(Number1, Number2, '+');
		}
		else if (response == 2) // Subtraction
		{
			correct_answer = Number1 - Number2;
			ask_question(Number1, Number2, '-');
		}
		else if (response == 3) // Multiplication
		{
			correct_answer = Number1 * Number2;
			ask_question(Number1, Number2, '*');
		}
		else
		{
			cout << "ERROR! Invalid choice. Please re-run program" << endl;
			return 1;
		}
	    
		int user_answer;
		cin >> user_answer;
	    
		if (user_answer == correct_answer)
		{
			cout << "CORRECT! \n";
			if (i == 1)
			{
				points += 10;
			}
			else if (i == 2)
			{
				points += 5;   
			}
			else if (i == 3)
			{
				points += 2;   
			}
			break; // break out of loop early since user got it correct
		}
		else
		{
			cout << "WRONG! ";
    			if (i == 1)
    			{
    				cout << "TRY AGAIN.\n";
    			}
    			else if (i == 2)
    			{
    				cout << "LAST TRY.\n";
    			}
    			else if (i == 3)
    			{
    				cout << "GAME OVER!\n";   
    			}
		}
	}
	
	cout << "Your points: " << points << "/10" << endl;
}
Last edited on
Topic archived. No new replies allowed.