Hello Daniweb,
I'm obviously very new to the programming world. I'm trying to make the popular Guessing random numbers game. The things I'm having a hard time with is getting the input validation to work. When characters are input instead of numbers, the program goes into an infinite loop. I'm not sure how to fix it, I've spent the last couple days searching the web and my textbook for some answers. I've tried putting it in, to no avail.
Also, I need to do the following:
· Move the guess response (the part that determines if the guess is too high or too low) functionality into a function that you write. The function must have the prototype int ReviewGuess(int, int), where the function takes the random number generated by the computer as the first parameter and the number guessed by the user as the second parameter. If the numbers match, the function will return a value of zero. If the number is too high, the function will return a value of 1. If the number is too low, the function will return a value of -1. If the user entry is not a valid value, return -2.
· Replace this code in the body of your program with a switch statement. When the user successfully guesses the number, prompt if he or she wants to play again. If so, have the computer choose a new random number.
Here is the code I have so far. The program compiles and runs fine, unless someone enters a charaacter instead of an integer.
#include <iostream> // Used to produce input and output information.
#include <cstdlib> // This header is used to produce the random number generator function.
#include <ctime> // Header used to help with the production of random numbers.
#include <limits> // Header used to help with setting limits to what kind of inputs are allowed
using namespace std; // Allows the use of cout and cin without having to type std in front of them everytime.
int main ()
{
bool playAgain=true;
while(playAgain)
{
int secretRandom = 0, userGuess = 0; //
const int MAX = 100, MIN = 1; // Sets limits on the random number generator and the range of guesses allowed by the user.
// Create random number
srand(time(NULL)); // Part one of random number generator.
secretRandom = (rand() % (MAX - MIN + 1)) + MIN; // Part two of random number generator.
cout << "Welcome to My Number Generator Game" << endl; // output
cout << "The object of the game is to guess a number between 1 and 100" << endl << endl; // output
// Loops forever until user finds the number
while (userGuess != secretRandom)
{
cout << "Please enter your guess : " ; // output
cin >> userGuess; // Input - User to enter a guess between 1 and 100
cin.ignore(numeric_limits<int>::max(), '\n');
if (!cin || cin.gcount() != 1)
{
cout << "ERROR!! Please enter a NUMBER between 1 and 100" << endl;
cin.clear();
}
else
{
if (secretRandom < userGuess)
{
cout << "Your guess of " << userGuess << " is too high. Please guess a LOWER number." << endl << endl; // output
}
else if (secretRandom > userGuess)
{
cout << "Your guess of " << userGuess << " is too low. Please guess a HIGHER number." << endl << endl; // output
}
else
{
cout << " Congratulations, you are CORRECT with your guess of " << userGuess << " !" << endl << endl; // output
}
}
}
// This next section gives the user the opportunity to play again
char again;
cout << " Do you want to play again? Enter y or n: " << endl;
cin >> again;
if(again!='y')
{
playAgain = false;
cout << endl << " Thank you for playing My Number Generator Game! " << endl << endl;
}
}
char yourName;
cout << "Please type your first name and press enter." << endl << endl; // output
cin >> yourName; // Input - User to enter in their first name
cin.clear();
cin.ignore();
return 0;
}