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, but I get errors at lines 28, 36, and 44. The program still works up until the point in the code that asks if the number is higher or lower. It works the first time you answer, so if you pick the number 700 for example, the program will always guess 500 first. You input 2 because your number is higher than 500. The program would then change the variables, making the program add 500 + 1000 and dividing by 2, giving 750. Then you would select 3, because your number is lower than 750. But instead of doing the right equation to guess correctly, it does the equation that it would use if you selected the Higher option. Adding 750 to 1000 and dividing by 2, giving 875 instead of what should be 625 by changing the high variable to 750 and adding 500 to it and dividing by 2. Any help would be greatly appreciated, and if I need to elaborate more, say so and I'll do my best to further explain. Here's my code, have at it.
#include #include <iostream>
#include <cstdlib>
#include <time.h>
using namespace 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;
}
Aucun commentaire:
Enregistrer un commentaire