This topic is 4612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

So I am going through the "Beginning C++ game programming book." In chapter 2 he asked me to write a program that has the user enter a number and then the computer has to guess it. I just finished it up and wanted to make sure my logic is all there. I think it works just fine, but let me know what you all think. Also I would think a recursive function would make alot more sense, but I guess it is only chapter 2 and I am following the book. So anyways, let me know what I could do to improve the code. Thanks all! Grey
//Guess the number the user puts in

#include <iostream>

using namespace std;

int main()
{
int number = 0, guess = 50, oldGuess = 100, tries = 1;
char response = 'y';

cout << "Please enter a number" << endl;
cin >> number;

cout << "Is the number " << guess << " (y/n)" << endl;
cin >> response;

if(response == 'n')
{
while(number != guess)
{
if(number < guess)
{
oldGuess = guess;
guess = guess/2;
}
else
{
guess = ((oldGuess - guess)/2) + guess;
}
tries++;
}
}
cout << "Your number is " << guess << endl;
cout << "I got it in " << tries << " tries" << endl;
cin.ignore(cin.rdbuf()->in_avail() + 1);
return 0;
}



Share on other sites
Hey Grey,
Im also going through that book and with the guess my number game as the original was to do with generating a random number to guess making the computers guess more of a random affair i think improve it and i think thats what it was trying to get you to do. Also just noticed in your code you dont tell the computer wether it was too high or too low and would fit in more with the original game if added.

**EDIT** just tried your code and it has a few probs:-
49
Is the number 50 (y/n)
n
I got it in 7 tries

as you can see it got it on its second guess and it did on every try but it always tells you it got in 6 or 7 tries.

[Edited by - dek001 on July 10, 2005 12:34:16 PM]

Share on other sites
The main() function seems to be doing a bit too much, you should try splitting it up into smaller well named functions. A good function name can be better than a comment, sticking everything in main() seems to throw away a valuable opportunity to increase the readability of the program.

//Guess the number the user puts in#include <iostream>using namespace std;int RequestNumberFromUser(){  int number = 0;  cout << "Please enter a number" << endl;  cin >> number;  return number;}void OutputGuessingResultsToUser(int NumberOfTries, int OriginalNumber){  cout << "Your number is " << OriginalNumber << endl;  cout << "I got it in " << NumberOfTries<< " tries" << endl;}bool IsInitialGuessCorrect(int InitialGuess){  char response = 'y';  cout << "Is the number " << InitialGuess << " (y/n)" << endl;  cin >> response;  return response != 'n';  }int FigureOutTheNumber(int InitialGuess, int NumberToGuess){  int guess = InitialGuess, oldGuess = 100, NumberOfTries = 1;  while(NumberToGuess!= guess)  {    if(NumberToGuess < guess)    {     oldGuess = guess;     guess = guess/2;    }    else    {     guess = ((oldGuess - guess)/2) + guess;    }    NumberOfTries ++;  }  return NumberOfTries;}void PreventWindowFromClosing(){  cin.ignore(cin.rdbuf()->in_avail() + 1);}int main(){	  int InitialGuess = 50;  int NumberToGuess = RequestNumberFromUser();	  if(IsInitialGuessCorrect(InitialGuess) == false)  {     int NumberOfGuesses = FigureOutTheNumber(InitialGuess, NumberToGuess);     OutputGuessingResultsToUser(NumberOfGuesses, NumberToGuess);  }  PreventWindowFromClosing();  return 0;}

You can figure out what the main function does just by reading the variables and functions now, its almost academic how the functions achieve their results.

[smile]

Share on other sites
Hey Jingo as its only the second chapter of the book it hasnt got into functions yet that doesnt come until chapter 5 but not everyone is to know that if they dont have the book.

This is the original game he's re-writing:
// guess my number// classic number guessing game#include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main(){  srand(time(0));// seed random number  int theNumber = rand() % 100 + 1; // random number between 1 and 100  int tries = 0, guess;  cout << "\t***WELCOME TO GUESS MY NUMBER***\n\n";  do  {    cout << "Enter a guess: ";    cin >> guess;    ++tries;    if (guess > theNumber)        cout << "Too High!\n\n";    if (guess < theNumber)        cout << "Too Low\n\n";  }while (guess != theNumber);  cout << "\nThats it, you have got it in " << tries << " guesses!\n";  cin.ignore(cin.rdbuf()->in_avail() + 1);  return 0;}

Share on other sites
Well keep it in mind for when you get to chapter 5 then. [smile]

Share on other sites
Some other thoughts are that the code that I moved to the IsInitialGuessCorrect function seems a bit flakey, and could possibly be removed.

Another problem is that you dont want the initial guess to be the same every time, perhaps you should use the rand() function as the original game code does for the initial number. That would prevent the number of tries being one each time you enter a number of 50.

On a more C++ side of things, you should really be checking the cin stream for error conditions after input. If the user enters a load of characters instead of a number for the initial input, the next cin will not work. A simple helper function could fix that.

void CleanInputStreamState(){ if(cin.good() == false) {   cin.ignore(cin.rdbuf()->in_avail());   cin.clear(); }}

You would then call this after each cin, and it will clear up the stream for you. This stuff may be a bit later in the book though. [smile]