So I started programming a few days ago and have trouble with this simple C++ program. Can anyone tell me how to fix it (Output should be 1 instead of 0). Thanks in advance :)
One is at line 10, in the if block. Inside the if block, this i hides the first one. So, inside the block, anything you do to i will be to this second variable.
However, once the block finishes, that second i is destroyed. Outside the block, the first i is used, and that never had its value changed, so it's still 0.
Solution: remove the int from line 10. That way, you're looking at the variable you declared at line 7 all the way through your program.
2) You've confused =, the assignment operator, with ==, the equality test operator. In line 10, you're not checking that the value of i is 0, you're setting it to 0.
The gotcha is that the expression (i = 0) evaluates to the value of i, which is 0. And in a boolean statement, 0 means false - so the if block won't be executed, and the else block will.
So, that's two things you need to change.
EDIT: Ninja'd by several people! That's what I get for writing a long post explaining things...