Hey all,
I just wanted to know if I should use a different process to run my loop. I use a while loop until the AI figures out the number.
I know there is a more efficient and easier way to make this loop happen with BOOLEAN.
Setting while(bool = 1) and having if/else/switches to change the bool = 0 to quit the loop.
When I do have a condition to set the loop = 0 (false), my loop continues to run though!
Can anyone provide me with a fix, or suggestion that I do not know about?
Thanks!
#include <cstdlib>
#include <iostream>
using namespace std;
int Toohigh(int x, int y);
int Toolow(int x, int y);
int main(int argc, char *argv[])
{
int number = 19; //INTERCHANGABLE THE MAGIC NUMBER (1-100)
int MAX = 100;
int MIN = 1;
int guess = 0;
int counter = 1;
do
{
if (number < guess){
MAX = guess; // Changes MAX range
guess = Toolow(MAX, MIN);
cout << "The computer is too high\n";
} //Too Low
else if (number > guess){
MIN = guess; // Changes MIN range
guess = Toohigh(MAX, MIN);
cout << "The computer is too low\n";
} //Too high
cout << "Attempt number: " << counter <<
"\tComputer guesses the number " << guess << endl;
counter++;
}while(number != guess);
if (number == guess){
cout << "The number is " << guess << " and the computer gussed it"
" on attempt #: "<< counter-1 << "!\n";
}
cin.get();
}
int Toohigh(int x, int y){ //(MAX, MIN)
int z;
z = x - y;
z = x -(z/2);
return z;
}
int Toolow(int x, int y){
int z;
z = x - y;
z = (z/2) + y;
return z;
}