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;
}

I have a few comments/suggestions.

1) user_guess is unused. You should setup your compiler to tell you about this - mine gave me a warning when I compiled your code.

2) There is no reason to use defines here - it is bad "c++ style".
At the least, change the defines to global variables:

#define MAXGUESS 100
#define MINGUESS 1
int MAXGUESS = 100;
int MINGUESS = 1;

at best, there is no reason to use global variables here at all!

3) You have a 110 line function (main). This is pretty long. I would consider breaking out functions like

bool IsGuessCorrect(int guess, int correctNumber);
char GetUserResponse();

The main function would then look like:

do
{
char response = GetUserResponse();
} while(response != 'y' && !IsGuessCorrect(guess));

(clearly not exactly like that, but the point is that the structure of the program is very simple, and main should reflect that.)

Good work!

David

Thanks for the tips Daviddoria! They are welcome. Like I said I am new to programming and we are just learning how to make our own functions now (probably why my main is 110 lines long lol). I will keep this in mind later down the road!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.