Hello Everyone,
I need to add functionality to store the guesses the user makes in an array up to a maximun number of guesses stored in a constant. It must display a list of the previous guesses before the user guesses a number each time (excluding their first guess). Once the user reaches the max number of guesses i must display a message and end the loop.
I am not feeling too good about this part of the program. I just don't know where to start on this portion.
Here is my current code:
#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.
using namespace std; // Allows the use of cout and cin without having to type std in front of them everytime.
int ReviewGuess(int secretRandom, int userGuess) // Function I created to check against certain conditions.
{
if (cin.fail ()) // This "IF" statement rules out entries that do not follow the rules of the games.
{
cin.clear();
cin.ignore(100,'\n');
return -2;
}
else if (userGuess < 1 || userGuess > 100) // This "ELSE IF" statement keeps integer guesses between 1 and 100.
return -2;
else if (secretRandom == userGuess) // this "ELSE IF" statement checks if the users guess matches the random number.
return 0;
else if (secretRandom < userGuess) // This "ELSE IF" statement checks if the user guess is higher than the random number.
return 1;
else if (secretRandom > userGuess) // This "ELSE IF" statement checks if the user guess is lower than the random number.
return -1;
}
int main ()
{
bool playAgain=true; // Boolean expression used to offer the ability to play the game again.
while(playAgain)
{
int secretRandom = 0;
int 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
int returnValue;
returnValue=ReviewGuess(secretRandom, userGuess);
switch (returnValue) // Switch statement with the directions for the user, based on the users guess.
{
case 0:
cout << " Congratulations, you are CORRECT with your guess of " << userGuess << " !" << endl << endl;
break;
case 1:
cout << "Your guess of " << userGuess << " is too high. Please guess a LOWER number." << endl << endl;
break;
case -1:
cout << "Your guess of " << userGuess << " is too low. Please guess a HIGHER number." << endl << endl;
break;
case -2:
cout << "ERROR!! Please enter a NUMBER between 1 and 100." << endl << endl;
break;
default:
cout << "Your guess is not within the guidelines of this game. Please enter a NUMBER between 1 and 100." << endl << endl;
}
}
// 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
return 0;
}