0

I am new to programming so our assignment was to create a number guessing game, where the user picks a number from 1 to 100. The initial guess in 50 and then the program asks the user if it is higher or lower than my guess. Once the answer is correct, the program displays how many tries it took in order to guess the answer. After struggling with it for awhile, I figured it out and then started to have some fun with it. Hope it helps someone!

```
#include <iostream>
using namespace std;
#define MAXGUESS 100
#define MINGUESS 1
int main()
{
cout<<"Welcome to the Guessing Game"<<endl;
int user_guess = -1;
int myGuess = 1;
int numberOfGuesses = 1;
int max=100;
int min=1;
char userReply = '\0';
bool gotIt = false;
cout<<"Think of a number between 1 and 100."<<endl;
//initial calculation of my guess
myGuess = (MAXGUESS-MINGUESS)/2+myGuess;
do
{
cout<<"Are you thinking of the number "<<myGuess<<"?"<<endl;
cout<<"Y for Yes, N for no: ";
cin>>userReply;
if (userReply=='N'||userReply=='n')
{
if((max<min)||(min>max)||(max==min))
{
cout<<"************************************"<<endl;
cout<<"* *"<<endl;
cout<<"* You are a damn liar! GAME OVER! *"<<endl;
cout<<"* *"<<endl;
cout<<"************************************"<<endl;
cin>>userReply;
return 0;
}
cout<<"Damn, Higher or Lower?"<<endl;
cout<<"H for Higher, L for Lower: ";
cin>>userReply;
if(userReply=='H'||userReply=='h')
{
if(myGuess<MAXGUESS) //myGuess must remain lower than 100
{
min = myGuess+1;
myGuess = ((max-min)/2)+myGuess; //binary search
if((max-min)==1) myGuess=min;
if(max==min) myGuess = max;
}
else
{
cout<<"You are a damn liar! GAME OVER!"<<endl;
cin>>userReply;
return 0;
}
}
else if(userReply=='L'||userReply=='l')
{
if(myGuess>min)
{
max = myGuess-1;
//min stays the same
myGuess = myGuess - ((max-min)/2);
if((max-min)==1) myGuess=max;
if(max==min) myGuess = max;
}
else if (myGuess==min)
{
cout<<"You are a damn liar! GAME OVER!"<<endl;
cin>>userReply;
return 0;
}
else
{
cout<<"You are a damn liar! GAME OVER!"<<endl;
cin>>userReply;
return 0;
}
}
else
cout<<"Don't understand input, try again."<<endl;
}
else if(userReply=='Y'||userReply=='y')
{
gotIt = true;
if (numberOfGuesses==1)
cout<<"I found your answer on the first try!"<<endl;
else
cout<<"I found your answer in "<<numberOfGuesses<<" tries"<<endl;
}
else
{
cout<<"Don't understand input, try again."<<endl;
}
/***************DEBUG**********************/
//cout<<" max: "<<max<<endl;
//cout<<" min: "<<min<<endl;
//cout<<" myGuess: "<<myGuess<<endl;
/***************DEBUG*********************/
numberOfGuesses++;
}
while (!gotIt);
cin>>userReply;
return 0;
}
```