guessing game

I'm trying to write a program for a guessing game where the user enters a number and tries to guess what number I have chosen. After the guess is entered the program should respond either your guess is too low, your guess is too high or congratulations you have guessed my number. I have to use the random generator to generate an integer. Here is the code I have written, but it's still not working right. I also have to check for invalid inputs. Can someone help me?


#include<iostream> // This is required to perform C+ stream I/O
#include<ctime> // This contains the prototype for the function time
#include<cstdlib> // Required for the exit function
#include<iomanip>
using namespace std;

int main()
{


// define variables
int numGuesses = 0; // This is a counter for the number of guesses
double myNumber = 77; // This will be the number I want the user to guess
char guess = ' '; // This is the number the user is guessing
double numReturned = guess; // This is the call random generator and gets a value between 1 and 100
int count = 0; // This is the counter for the number of guesses
double num = rand(); // This is for a number
char Play = 'y'; // 'n'entered will stop the program


cout << "\nWelcome, can you guess what number I am thinking of? ";
cout << "\nIt's a whole number between 1 to 100. How many guesses will you need: ";
cin >> numGuesses;
cout << "\nLet's begin! Enter your guess: ";
cin >> guess;


{
while ((myNumber > guess) && (myNumber != guess ))
{ count++; }
cout << "\nYour number is too low!" << endl;

if ( myNumber > guess && myNumber != guess )
{ count++; }
cout << "\nYour number is to high!" << endl;

if
( myNumber == guess && numGuesses != count )
{ count++; }
}
numReturned = rand() % 100 + 1;
srand(time(NULL));





cout << "\nCongratulations you guessed my number!" << endl;
cout << "\nMy number is: " << myNumber << endl;

cout << "\nDo you wish to play again? (y = yes, n = no) "<< endl;
cin.get(Play);

// check for valid entries
while (tolower(Play) != 'y' && tolower(Play) != 'n')
{
cout << "\nInvalid response, (y = yes, n = no): "; // if user enters invalid response
cin.sync();
cin.clear();
cin >> Play;
}
system("cls"); // clears screen
// end invalid input check

return 0;
}
Because guess is a char when it should be a double. When comparing variables always compare apples with apples.

Also your while statement is a mess, you should nest the two if statements within a larger while statement with a break for the '==' one. Example:

1
2
3
4
5
6
7
while(...)
{
if(...)
{}
if(...)
{}
} //End While 
Last edited on
The code needs code tags, and it also needs indentation, and you need to describe what your errors are.
What constitutes an invalid input? (Besides the y/n bit, anything else that would be invalid?)
Here is a complete example with input checking. Hope you learn something.
Here is an example of error checking

Maikel

1
2
3
4
5
6
7
        cin >> guess_num;
        while (cin.fail()) {
            cout << "Type an integer: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cin >> guess_num;
        }
Last edited on
Hope you learn something

Come on.... surely you know better than to just post the answer.
Sorry.
Topic archived. No new replies allowed.