Hey there -- trying to write a program that allows the player to pick a number, and then tells them if it's too low or too high until they get the right answer. When they have the right answer, the program should tell them how many tries it took. My code so far is below. It works for positive numbers, but if the user picks a negative number, the loop breaks prematurely and gives the wrong answer about attempted guesses.
Your if statements will mess up the program and end it when it shouldnt, here is why.
Let's say n = 55;
My first Guess is 66. First if-statement is entered because 66 is bigger than 55. Inside that if-statement I can guess again. I pick 66 again. Now it checks if guess is smaller than n, which it's not. So it goes to the else statement and it ends. That's not what we want is it?
Change the middle if-statement to and else if statement.
elseif (guess<n)
to avoid that problem.
Edit 1:
Instead of having 2 different cin >> guess; You could simply just have one at the top of the do-while loop, which would allow you to remove the ones inside the if statements and the one above the do-while loop. And the program would do the exact same thing.
Also I should mention that I never had the problem you described, It worked perfectly fine with negative numbers and Im having a hard time seeing why it would break prematurely for you. Try to make these changes I told you about and see if it fixes it.