Basic Guess My Number

Jul 14, 2010 at 11:10pm
Hello,
I recently made some code for a simple guess my number program. However, if you guess it wrong, it keeps on printing "Sorry, guess again." What did I do wrong?


void main()
{
printLine ("Please write the meaning of life, the universe, and everything");
int var1 = 42;
int var2 = 0;

var2 = readInt();
while (var1 != var2)
{
printLine ("Sorry, guess again");
}
printLine ("You guessed it!");
}
Jul 14, 2010 at 11:15pm
1
2
3
4
while (var1 != var2)
{
    printLine ("Sorry, guess again");
}


Please use code tags in the future. Now tell me, how will the above loop ever end? When will either var1 or var2 ever change?
Jul 14, 2010 at 11:22pm
Right.
var2 changes when the user inputs a value through the function readInt. I don't know at the moment how to stop the loop from repeating itself. var1 does not have to change, I could use randomInt for that, but I want to keep this as simple as possible right now.
Jul 14, 2010 at 11:26pm
Oh... Should I have the readInt inside of the loop?
Jul 15, 2010 at 6:27am
Your program is set to loop "Sorry, guess again" While var != 42 and that's it.

The user isn't given an option to change his answer because it isn't in the loop.

Topic archived. No new replies allowed.