# Thread: help with making a basic game

1. ## help with making a basic game

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:

Code:
```#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
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;
}```
any help would be greatly appreciated

2. you could have just said binary search instead of typing all that.

edit: Your if statements are not doing anything. Your while loops are incorrect if the if statement applies to it. Does this compile with your compiler?

edit2: it is the ; after the if statements. Travis Dane beat my edit.

3. You don't need an ";" after if statements like this

Code:
`if (right == 2 && guess < max && number != guess); // no ";" required`
Also

Code:
```       cout <<"My next guess is " <<guess <<". Type 1 if I'm right, 2 if too high" >> /* <- you forgot that */
" and 3 if too low." <<endl;```

4. Code:
```#include <iostream>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;

void clrscr(void)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

COORD coordScreen = {0, 0};
DWORD cCharsWritten, dwConSize;
CONSOLE_SCREEN_BUFFER_INFO csbi;

GetConsoleScreenBufferInfo(hOut, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

FillConsoleOutputCharacter(hOut, ' ',
dwConSize, coordScreen, &cCharsWritten);

GetConsoleScreenBufferInfo(hOut, &csbi);

FillConsoleOutputAttribute(hOut, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);

SetConsoleCursorPosition(hOut, coordScreen);
}

int main()
{
srand(time(NULL));
int cmp, high = 101, low = 0;
char input[256];
bool GameOver = false;

cout << "Think of a number between 0 and 100...";
while(!kbhit());

while(!GameOver)
{
clrscr();

do
{
cmp = rand()%high;
}while(cmp <= low);

cout << "The computer guesses: " << cmp << endl;

cout << "Is this too [l]ow, too [h]igh, or [c]orrect? ";
cin.getline(input, 256, '\n');

if(toupper(input[0]) == 'L')
low = cmp;
else if(toupper(input[0]) == 'H')
high = cmp;
else if(toupper(input[0]) == 'C')
GameOver = true;
else
continue;
}

return 0;
}```