I am new to c++ and I'm kinda struggling. I'm trying to write a program to guess a number that the user inputs. The program should guess a number then the user should be prompted if it is too high or too low. then the program should guess again, intill it gets it right. Then display "I guessed your number of ( lets say 37) in (lets say 5) tries" it should always guess the users number in 7 tries or less. So first the user is prompted to pick a number between 1 and 100, then the computer should guess the number, the first guess should always be 50, the user is then asked if this is too high or too low or correct, then if high the computer should guess 75, then the user should be prompted again is the number high low or correct, so basically every time the computer guesses it should be between the last guess and the maxguess. once the user has indicated that the guess is correct the program should display "I guessed your number of " " in " " guesses!

Here is some pseudo code for the program
numberofTries 1
guess 50
highLowResponse
usersNumber
minGuess 0
maxGuess 100

prompt user for mumber
retrieve userNumber

check guess
if no match
numberofTries increment
ask if high or low
get highLowResponse
if high
maxGuess gets set to current guess
new guess is guess/2 + minGuess
if low
minGuess gets set to current guess
new guess is maxguess - guess/2

output guess and number of tries

So what i need is some help with the program itself and I would like to thank all of you in advance for your help. :)

Here is something that will start you off :

#include<iostream>

using namespace std;

int main()
{
     //Create a variable for   :
    // Min number the user can input
   //Max number the user can input
  //The number of guesses
 //The winning number

 //use random number generator to assign the winning number a value
 //From range [min,max]

 //Create a do while loop
  //Ask user for input
 //Check If its '>' ,  '<' , ' ==' the winning number and print certain statements accordingly

  return 0;
}

okay but what about loops, what kind should i use and i do i set them up? and a random number generator can not be used in this particular program, all numbers used should be predetermined in the code.

Use a do while loop

int winningNumber = 42;
int guessNumber = -1;
do
{
   //ask user to guess a number
  //check if its equal or not. Then determine the steps from there
}while(winningNumber != guessNumber)

okay, here is what i have so far,

#include <iostream>

using namespace std;

int _main
{
	int theNumber;
	int computerGuess;
	int numGuesses = 1;
	char answer;
	int max;
	int min;
  
        cout << "Enter a number between 1 and 100:" << endl;
		cout << "Press h if my guess is high or l if my guess is low." << endl;
		cin >> theNumber;
        
	computerGuess = 50;

	while (numGuesses <= 5)

	{
		 cout << "I guess " << computerGuess << endl;
		 cin >> answer;

		 if (answer == 'l')
		 {
			 max = 100;
			 min = computerGuess + 1;
			 computerGuess = (max + min)/2;

			 numGuesses ++;
		 }

		 else if (answer == 'h')
		 {
			 max = computerGuess;
			 min = 0;
			 computerGuess = (max + 1)/2;

			 numGuesses ++;
		 }
	}
		return 0;

}

The problem i'm having now is the if you enter a number like lets say 77 the computer guesses 50 first like it should then the user enters l for low then the computer guesses 75 once again like it should, again the user enters l for low then the computer guesses 88, then the user enters h for high and the computers next guess is 44 but it shouldn't guess anything lower than 75 because it has already been determined that the number is higher than 75. so how can i fix this. additionally i need a way to tell the computer that the guess is correct once this happens.

OH, I think you don't need have the user input a number. You need
to just have the computer guess a number, and the user should say
if its high,low, or correct.

In that case, you should have a function called average(int,int), that
takes 2 ints and returns its average. The two ints passed will
be your min and max.

min = 0;
max = 100;
int avg = average(min,max); //50

Now see if the average is correct. If its too low
then make you min = average, and else if its too high make your
max = avg, else its correct. From there try to figure out the rest.

well the only problem with that is we haven't covered functions yet, so I cant use one in this code. I know the problem is between lines 26 and 39 and im pretty sure its in the computer guess line.

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int theNumber;
	int computerGuess = 50;
	int numGuesses = 1;
	char answer;
	int max = 100;
	int min = 0;
	
        cout << "Enter a number between 1 and 100:" << endl;
		cin >> theNumber;
        
	computerGuess = 50;

	while (numGuesses <= 7)

	{
		 cout << "I guess " << computerGuess << endl;
		 cout << "Press h if guess is high,l ifguess is low, or c if correct." << endl;
		 cin >> answer;

		 if (answer == 'h') 
		 {
			 max = computerGuess - 1;
			 min = 0;
			 computerGuess = ((max - min)/2 ) + min;
			 numGuesses ++;
		 }

		 else if (answer == 'l')
		 {
			max = 100;
			min = computerGuess + 1;
			computerGuess = (max + min)/2;
			numGuesses ++;
		 }
		 else if (answer == 'c')
		 {
			 cout << "I guessed your number of " << theNumber << " in " << numGuesses << " guesses!" << endl;
		 }
	}
		return 0;

}

You need to change the limits of min or max depending whether
the guess number is high or low.

There is 1 bug in this code. I'll let you figure that out

do
	{		
		cout<<"Is your number : "<< mid << "  ?  \n\n";
		cout<<"Too (h)igh\n";
		cout<<"Too (l)ow\n";
		cout<<"(c) orrect\n";
		cout<<"(input) : ";
		cin >> pick;

		switch(pick)
		{		   //if its too high then now max should be mid and the guess number should be from 0 to mid or min to max
		case 'h' : max = mid;
				   mid = (int)ceil((min + max)/2.0);
				   break;

		case 'l' : min = mid;
				   mid = (int)ceil((min + max)/2.0);
				   break;

		case 'c' : gameOver = true; break;
		}
		
		cout<<"\n\n";

	}while(!gameOver);
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.