Michael Scalzenni wrote:Hello, I was trying to make a really simple subtraction test that loops and keeps giving you questions until you get one wrong, I also wanted a counter at the end to tell you how much you got right before you messed up. I tried putting the code in a while loop but it didn't work so I put it in a if loop, here is what I have so far...
Paul Clapham wrote:If you want a loop which repeats something forever the easiest way is like this:
You may find you don't really want it to repeat forever, you might want it to stop when some condition happens (like the user learns how to subtract correctly). In that case you should use a boolean variable to keep track of that, like sal jefferson suggested.
sal jefferson wrote:You are still using
Change this to
sal jefferson wrote:You are assigning user_answer to the value of Answer. So no matter what the user_answer was, it will be changed to Answer, thus the user will never be wrong and the loop will go on forever.
user_answer = 6;
Answer = 100;
user_answer == Answer;
user_answer = 100;
No, you aren't. You are using == there to test for identity. In that instance, if you mistakenly wrote = you would suffer a compile‑time error.
sal jefferson wrote:. . . here
You are assigning user_answer to the value of Answer. . . .
The OP is not trying to reset testing; it is a mistake while trying to write == true. We see that sort of error every now and again.
sal jefferson wrote:. . .
So you wouldn't need to set it to true again here
. . .
…or similar becuse I had intended to write ==. The only time you can get that sort of thing to compile (as before) is with two booleans, in which case you get two logic errors for the price of one.
Expected boolean, found int
And I was mistaken; a lot of primitives are incompatible with other types and x == y can fail to compile.
A few minuste ago, I wrote:. . . It will always compile . . ..