### A bit of help understanding if then else statements.

So I'm trying to complete as many practice c++ programs as I can because my programming class is extremely slow and we aren't learning anything.
I'm writing a program that asks the user a couple of math questions then outputs if they were right or wrong and at the end outputs their score.

However the problem I'm having is that if the user answers correctly both outcomes are outputted anyway. *Note: if the user answers correctly it still adds one to their score at the end.*

Also on line 19 and 30 there are two semicolons that have to be there so it won't throw errors but don't make sense (why does the else have to have them but not the if?). I wrote another program with some if then else statements and didn't have them there.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include using namespace std; int main() { float score = 0; int ans1; int ans2; cout << "What is 12 + 14? " << endl; cin >> ans1; if (ans1 == 26) { cout << "Nice, you got the correct answer." << endl; score ++; } else (ans1 != 26); { cout << "Wrong, the answer is 26." << endl; } cout << "What is 10 +13? " << endl; cin >> ans2; if (ans2 == 23) { cout << "Yeah, the answer is 23. " << endl; score ++;} else (ans2 != 23) ;{cout << "That is not correct, the answer is 23. " << endl; } cout << "Your score is: " << score << endl; return 0; }``````
Last edited on
I am fairly new to programming, but i believe if you write "else if" in place of your "else" then it should be fixed
Your first if statement looks like this:
 ``1234567`` `````` if (ans1 == 26) { cout << "Nice, you got the correct answer." << endl; score ++; } else (ans1 != 26);``````

Notice the `else` clause doesn't actually do anything. It's just a passive expression.

It should look like this:
 ``123456789`` `````` if (ans1 == 26) { cout << "Nice, you got the correct answer." << endl; score ++; } else { cout << "Wrong, the answer is 26." << endl; }``````
A suggestion, when you get this working, rather than hard-coding the questions and answers into the program, get the computer to generate a couple of random numbers, and perform the addition itself. That way, the questions will be more varied, and you don't have to pre-calculate the answer, let the computer do the work for you.
The else can't have a condition associated with it - or a semicolon. Just change it to this:

 ``123`` ``````else { cout << "Wrong, the answer is 26." << endl; }``````

Notice how I did the braces - the closing brace should line up with the statement it belongs to.

Same story for lines 29 & 30

 http://www.cplusplus.com/doc/tutorial/

Hope all goes well
Oh ok, I get it, that makes more sense (with the no else condition) and chervil ok I'll start doing that now; is there a way to limit the random numbers it generates? I'm not trying to have 434 *234 to do in my head haha.

Maybe like fixed << setprecision(1) ? and change the int to a float variable?

Thanks again!
Regarding random numbers, it's common to use the modulo operator % to limit the range. See reference for examples: http://www.cplusplus.com/reference/cstdlib/rand/
Big help guys, thanks!
why is your 'score' variable a float? Just make it an int since it will only go to 2..

i read somewhere that it's bad style to use the ++ incrementer for anything other than int, however you'll have to google it to find out why as i can't remember
Topic archived. No new replies allowed.