I'm trying to make a program that will guess a number you input. I'm kind of confused on what I'm doing wrong. The program compiles, and everything works, but to a point. The program works until it asks you for an input after it guesses 2 times. On the 3rd time the program exits. For example say my number is 700. The program will always guess 500 the first time. Since 700 is more than 500 you would select the option for "Higher" (Code below). The program would change the variable "Low" From 0 to 500, the current guess. Then it would add the "Low" to the "High" (1000 at the first guess) and change the answer to the "Guess" variable. Then it would take the "Guess" variable and divide it by 2, making this the new guess, then printing it out as a guess in the program. So if the number was more than 500, the second guess would be 750. Now this time the equation will change. It will change the "High" variable to the current guess, and add the "Low" to the "High" once again. The answer will be changed to the guess and then divided by 2 once more, giving 625. Since 700 is still higher than 625, it will again use the equation from the first input. But instead of doing this, the program just exits. Any help? If I need to further elaborate please say so. I'm assuming I'm doing something wrong in the if loop where I have
#include <iostream>
#include <cstdlib>
#include <time.h>
usingnamespace std;
int main ()
{
int number,low,high,guess,guesses,status;
low = 0;
high = 1000;
guesses = 0;
cout<<"Please pick a number between 1 and 1000 \n";
do
{
cin>>number;
if (number > 1000) cout<<"Please pick a number less than 1000 \n";
} while (number > 1000);
cout<<"Thank you. Now I will guess your number! \n";
guess=500;
cout<<"Is your number "<<guess<<"? \n";
cout<<"[1] Yes \n"<<"[2] Higher \n"<<"[3] Lower \n";
do
{
cin>>status;
if ( (status != 1) && (status != 2) && (status != 3) ) cout<<"Please select option 1, 2, or 3 \n";
} while ( (status != 1) && (status != 2) && (status != 3) );
do
{
if (status == 2)
low=guess;
guess=low+high;
guess=guess/2;
guesses=guesses+1;
cout<<"Is your number "<<guess<<"? \n";
cout<<"[1] Yes \n"<<"[2] Higher \n"<<"[3] Lower \n";
cin>>status;
if (status == 3)
high=guess;
guess=low+high;
guess=guess/2;
guesses=guesses+1;
cout<<"Is your number "<<guess<<"? \n";
cout<<"[1] Yes \n"<<"[2] Higher \n"<<"[3] Lower \n";
cin>>status;
if (status == 1)
cout<<"I guessed your number! It only took me "<<guesses<<" tries to guess it!";
} while ( (status == 3) && (status == 2) && (status == 1) );
return 0;
}
#include <iostream>
usingnamespace std;
int main ()
{
int number,low,high,guess,guesses,status;
low = 0;
high = 1000;
guess = 0;
guesses = 0;
do
{
cout<<"Please pick a number between 1 and 1000 \n";
cin>>number;
if (number > 1000) cout<<"Please pick a number less than 1000 \n";
}
while (number > 1000);
cout<<"Thank you. Now I will guess your number! \n";
guess=500;
do
{
do
{
cout<<"Is your number "<<guess<<"? \n";
cout<<"[1] Yes \n"<<"[2] Higher \n"<<"[3] Lower \n";
cin>>status;
if ((status < 1) && (status > 3)) cout<<"Please select option 1, 2, or 3 \n";
}
while ((status < 1) && (status > 3));
if (status == 2)
{
low=guess;
guess=low+high;
guess=guess/2;
guesses++;
}
elseif (status == 3)
{
high=guess;
guess=low+high;
guess=guess/2;
guesses++;
}
if (status == 1)
{
guesses++;
cout<<"I guessed your number! It only took me "<<guesses<<" tries to guess it!";
break;
}
}
while ( (status > 0) && (status < 4) );
return 0;
}
One big problem is you are having logic errors because of your integer math. Dividing integers truncates any remainder. I tried 250 and the program went into an infinite loop guessing 249.
Some of the if statements need braces around the block of statements controlled by that condition.
A do - while loop with this condition is going to present a problem: while ( (status == 3) && (status == 2) && (status == 1) );status cannot simultaneously hold three different values.
As for the integer arithmetic, it shouldn't be a problem in this context, if used with care. That may require adjustment of some of the code.
Also, I'd try to minimise repetitive code. Within the main guessing loop, I'd aim for just one cin >> status and just one guesses=guesses+1; (or preferably guesses++;).
Edit: One more comment. After an incorrect guess, instead of setting either low=guess; or high=guess;, it is better (and should eliminate an infinite loop) to use low=guess+1; or high=guess-1; as appropriate. There's no need to include guess itself in the range as it is already known to be wrong.