wrong value when adding random intergers

Hi, I have this calculator assignment to do and it is going along fine I have about 70% done but I am stuck on a certain part. When I add my two values that I obtained from a random integer I get this crazy number instead of the number that I expected. It is when I get the value CorAns. I marked it with HERE. I know it's not the switch statement itself because if I add it normally it will still give me a weird answer.

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
#include <iostream>
#include <iomanip>
#include <ctime>
#define SENT 4



int drill1prob(int c, int r1,int r2)

{
    using namespace std;
    int reply;
    int CorAns;
    
     switch (c)
    {
    case 1 : CorAns = (r1 - r2) ; break;
    case 2 : CorAns = r1 + r2 ; break;       // HERE
    case 3 : CorAns = r1 * r2 ; break;
}
   
   
  
    if (c=1)
   { cout << r1 <<" - "<<r2 << "  "; 
    cin >> reply; }
    else if (c=2)
   { cout << r1<<" + "<<r2 << "  "; 
    cin >> reply; }
    else if (c=3)
   { cout << r1<<" * "<<r2 << "  "; 
    cin >> reply; }
    else return r1;
    
    if(reply==CorAns)
    cout <<"Yes,That is correct. Good Job!" ;
    else
    cout <<"No,the correct answer is" << CorAns ;
    return r1;
    
}
int Gen2Rand()



Last edited on
On both line 13 and line 78 there are variables named CorAns, that may be a cause of confusion as these are separate and unrelated, apart from having the same name.



Perhaps of more immediate concern is the use of = instead to == when testing for equality.

= is the assignment operator, here c is set to the value 1, if (c=1) and the result is always true.
Last edited on
Wow, Thank you very much. I had somehow fixed the previous problem I had by doing some editing to make it look more clean. The next problem I had was the one you had just given me the answer too. The calculator works great now but it kept on displaying the minus in the equation even when it wasn't. Again thank you.
Last edited on
Topic archived. No new replies allowed.