guessing game

I posted this under beginners also then realized there is a forum for C++ programming.

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;
}
Don't double post.
Topic archived. No new replies allowed.