### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include #include #include #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 <<" - "<> reply; } else if (c=2) { cout << r1<<" + "<> reply; } else if (c=3) { cout << r1<<" * "<> 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.