Basic question to do with operators.

Hi. I'm trying to make a random math question generator. I generate 2 random numbers between 1 and 30 along with a random operator. Here is the code i use to check if the answer is right.

1
2
  if (useranswer == setnumone setoperator setnumtwo){
cout "good job u got it right!"


setoperator is a 'char' variable which can either be + or -(which is determined by a previous randomly generated number). setnumone and setnumone are the random numbers between 1 and 30. However, every time i try to see if the answer is correct, I get an error?
Thanks
Use a switch statement to get the result using the desired operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
switch(setoperator)
{
   case('+'):
   {
      result = setnumone + setnumtwo;
      break;
   }
   default: // '-'
   {
      result = setnumone - setnumtwo;
      break;
   }
}

Then compare the useranswer with the result
I see, thanks for this. However, is it possible to output the question through this?
Line 1: You can't have multiple fields on the right hand side of a comparison operator.
 
 if (useranswer == setnumone setoperator setnumtwo)

i.e. the underlined portion makes no sense.

You also haven't shown us enough code for us to know the type of your variables. In the above snippet, useranswer and setnumone must be compatible types. i.e. Types that can be compared. Comparing a string and an int is not going to work.

Line 2: You're missing the output operator.
 
cout << "good job u got it right!"



Topic archived. No new replies allowed.