Hello,
I am new to C++ and I am trying to create a program but I am not sure where to start exactly.

Program purpose : Person/Player is asked to enter a number between 1 and 100. The computer randomly guesses a number and the player says whether its (h)igher or (l)ower or (c)orrect.
The computer has 6 tries to guess the number or the player wins. If the computer guesses the number in 6 tries then the player loses.

I am not really sure how to go about doing this.

I know I have to declare a value for MIN and MAX as well as creates a variable for the number of tries and the player number.

Any help is appreciated. Thanks!

Recommended Answers

All 7 Replies

If I was writing this this I would strive for simplicity. Have the user enter a value and then have the computer guess 50, the program should respond by indicating higher or lower(or correct). The computer then should take the set above or below and pick a middle value...etc...etc.

Example:

user enter value 35.

Computer guesses 50.(guess 1)

Program responds too high.

Computer guesses 25.(guess 2)

Program responds too low.

Computer guesses 37(guess 3).

Program responds Too high.

Computer guesses 31(guess 4).

Computer responds too low.

Program responds 34(guess 5)...etc

Thanks,
Here is what the worksheet says that i got today.

The objective of this assignment is to develop and implement a simple guessing game.

The rules of the game are as follows:

* The user will be asked to pick a number in the range 1 through 100 (inclusive), and the computer gets six tries to guess which number the user picked.
* After each guess the user must indicate if the number they have chosen is lower than, higher than, or equal to the computer's latest guess.
* The player wins the game if the computer fails to guess the correct number in six tries, the computer wins otherwise.

The player should be able to play multiple games in a row, with the computer keeping track of how many games resulted in player wins and losses.

I really need some help with this. I am not looking for just the answer but an explanation of why the code is what it is.

I really need some help with this. I am not looking for just the answer but an explanation of why the code is what it is.

What code?

I really need some help with this. I am not looking for just the answer but an explanation of why the code is what it is.

What code?

What I meant was I need the code but I want an explaination of why the code is what it is.

I was more or less looking for someone to give me an example of the code and explain why it was suppose to be like that.

Sorry for the confusion.

Sorry...This isn't a 'give the code kind of website'...You have to show some effort and post the code you have..

Alright, here is what i got so far because I was not really sure how to incorporate the rand function. Any help is appreciated, I just want to get this working and hopfully understand how it works.

#include <iostream>
#include <cstdlib>


using namespace std;

int main()
{
int myNumber = 100;
int guess = 0;
int min = 0;
int max = 100;
char hint;

cout << "Input a number between 1 and 100: " << endl;
cin >> myNumber;
cout << "\nPress l if too low or press h if too high.\nPress c if correct " << endl;

while(guess != myNumber)
{
guess = ((max - min) /2) + min;
cout << "The computer guesses: " << guess << endl;
cout << "please press 'h' if high, 'l' if low and 'c' if correct: \n";
cin >> hint;



if(hint == 'h')
{
max = guess;

}
if(hint == 'l')
{
min = guess;

}
if (hint == 'c')
{
cout << "Game Over!" << endl;

}
}

system("pause");
return 0;
}

Here's a quick and incomplete solution to what I was referring to in the above posting:

#include <iostream>
#include <cstdlib>


using namespace std;

int main()
{
	int myNumber = 100;
	int min = 0;
	int max = 100;
	int guess = 0;
	int hint = 0;
	int count = 1;

	cout << "Input a number between 1 and 100: " << endl;
	cin >> myNumber;
	cout << "\nPress 1 if too low or press 2 if too high.\nPress 3 if correct " << endl;

	guess = ((max - min) /2) + min;
	cout << "The computer guesses: " << guess << endl;

	cout << "Please enter 1 for too low, 2 for too hign and 3 for correct->";
	cin>>hint;
	
	while (guess != myNumber)
	{
		switch (hint)
		{
				case 1:
				{
						cout << "too low" << std::endl;
						min = (max - guess + min);
					
						cout << "min set to->" << min << std::endl;
						cout << "counter->" << ++count << std::endl;
						break;
				}
				
				case 2:
				{
						cout << "too high" << std::endl;
						max = (max - guess + min);
						cout << "max set to->" << max << std::endl;
						cout << "counter->" << ++count << std::endl;
						break;
				}
				
				case 3:
				{
						cout << "correct" << std::endl;
						return 1;
				}
				
				default:
				{
						cout << "entered invalid character" << std::endl;
				}
			}
			guess = ((max - min) /2) + min;
			cout << "The computer guesses: " << guess << endl;

			cout << "Please enter 1 for too low, 2 for too high and 3 for correct->";
			cin>>hint;
	}
	return 0;
}

Please note that I keep cutting down the set of valid numbers, by half, as the computer guesses a middle value.

I noticed your user name...A canuck?

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.