ok, im trying to make a reverse guessing game type of thing, where you think of a number (1-100) and the program tries to guess it. basicly it starts by guessing 50, and you tell it if its too high or too low. if its too high, then obviously your number is 1-49, so it sets an int variable called "max" to 50 (since the number isnt any higher than this) it does the opposite thing (with variable "min") if you say that 50 was too low. the guess is halfway between wut it just guessed and either 0 or 100, depending on wut you said. (ie, if you said 50 was too high, it will guess halfway between 50 and the min [so if the min was 0, halfway between 0 and 50 is 25], if you said 40 was too low, it would guess halfway between 40 and the max [so if the max was 80, halfway between 80 and 40 is 60]). using this method, it should never go above the max or below the min, even if it may take a while to get the answer (like 49 or 51). however, sometimes it goes above the max or below the min, and i cant figure out why. here is my code:
any help would be greatly appreciatedCode:#include <iostream> #include <string> int main() { int number = 500;//Make sure it completes the first loop int guess = 50;//The first guess is always 50 int right = 0; //The variable to tell the program to raise or lower it's guess int tries = 0; //The attempts to get it right int min = 0; //The lowest number it has guesses that wasn't too high int max = 100; //The highest number it has guessed that wasn't too low for (;(number < 1 || number > 100);) //Make sure the number is valid { cout <<"Enter your number 1-100 for me to guess. (Dont worry I wont cheat)" <<endl; cin >>number; } cout <<"My first guess is " <<guess <<". Type 1 if I am right, 2 if I am too " <<"high and 3 if I am too low." <<endl; tries++; /*The first guess is 50 and adds one try, then asks if it it too high or too low*/ cin >>right; while (right != 2627) //Loop forever { if (number == guess) //If the program is right.... { cout <<"I was right. Go me! It took me " <<tries <<" tries!" <<endl; system ("pause"); return 0; //...It stops } if (right == 2 && guess < max && number != guess); /*The guess was lower than the max (defualt 100) and was too high, so it sets the new max*/ { max = guess; guess = (guess + min)/2; cout <<"My next guess is " <<guess <<". Type 1 if I'm right, 2 if too high" " and 3 if too low." <<endl; cin >>right; tries++; } if (right == 2 && guess >= max && number != guess); /*The guess was higher than the max (shouldn't happen, but just in case), so it doesn't set the max again.*/ { guess = (guess + min)/2; cout <<"My next guess is " <<guess <<". Type 1 if I'm right, 2 if too high" "and 3 if too low." <<endl; cin >>right; tries++; } if (right == 3 && guess > min && number != guess); /*The guess was higher than the min (defualt 0) and was too low, so it sets the the new min.*/ { min = guess; guess = (guess + max)/2; cout <<"My next guess is " <<guess <<". Type 1 if I'm right, 2 if too high" " and 3 if too low." <<endl; cin >>right; tries++; } if (right == 3 && guess <= min && number != guess); /*The guess was lower than the min (shouldn't happen, but just in case), so it doesn't set the min again.*/ { guess = guess; cout <<"My next guess is " <<guess <<". Type 1 if I'm right, 2 if too high" " and 3 if too low." <<endl; cin >>right; tries++; } if (right == 1 && guess != number); /*User says the program is right but it's not*/ { cout <<"No, I'm not right yet. Was " <<guess <<" too high (2) or too low" " (3)?" <<endl; cin >>right; } } system("Pause"); return 0; }