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;
}

I'm thinking I need to add in something like:

const int GUESSES = 10;
int guess[GUESSES];
int numberTries;

then add in some sort of info to the loop to get an output of the userGuess variables.

AHHHHH!!!

Not pertaining to your question, but important enough to point out...

You should move line 35 up to the top of main. It needs to be executed once and exactly once. Sticking it in the while loop as you do makes numbers less random, not more random.

As to your question regarding the array, there are several considerations.

  1. Do you want to declare the array as a global variable or do you want to declare it somewhere else and pass it to a function?
  2. Should the array be a static size array or a dynamically allocated array?
  3. How do you initialize the array and do you need to initialize it?

For numbers 1 and 2, make your life easy. Make the array global and don't make it dynamic. The fact that the array size is a constant is the tip-off.

You are correct in your approach. Stick this at the top, before any function:

const int GUESSES = 10;
int guess[GUESSES];
int numberTries;

But you need to initialize numberTries to 0 for each game somewhere.

Hence the answer to number 3. Don't bother initializing it. You only care what the array contains for indexes less than numberTries. Hence this problem is easier than many array problems.

So your approach should be:

  1. Start a new game by initializing numberTries to 0 and picking the random number.
  2. Display the list of guesses so far. This won't be done for the FIRST guess. Yes. You need a loop. If there have been 4 bad guesses, you need to display guess[0] through guess[3].
  3. User inputs guess.
  4. You check guess for wrong/right and display whatever message is pertinent.
  5. If wrong, stick the bad gues at the end of the array and increment numberTries.

Yet another Edit : Concerning point 5, note that the "end" of the array may not mean element 9. It is the "end" of the "relevant" part of the array (i.e. index is less than the number of wrong guesses).

Edited 5 Years Ago by VernonDozier: Removed redundant item in list.. Also tidied up grammar a little.

Should I move both line 35 and 36 up to thee top of main then, or just line 35?

Question 1: yes, I would rather it be a global variable.
Question 2: it must be a set size, I chose the size to be 10, and according to the directions, it must be an array, not a vector.
Question 3: not sure about this question.

I'm thinking that the following code should be placed early on the program, perhaps the beginning of the main function?

const int GUESSES = 10;
int guess[GUESSES];
int numberTries;

Question 1: yes, I would rather it be a global variable.
Question 2: it must be a set size, I chose the size to be 10, and according to the directions, it must be an array, not a vector.
Question 3: not sure about this question.

>> Should I move both line 35 and 36 up to thee top of main then, or just line 35?

You need to seed the random number generator once. You need to pick a random number once per game, so more than once. Hence leave line 36 where it is, move 35 up.

Question 1: yes, I would rather it be a global variable.
Question 2: it must be a set size, I chose the size to be 10, and according to the directions, it must be an array, not a vector.
Question 3: not sure about this question.

"Dynamic" doesn't necessarily mean vector. You don't want it dynamic anyway.

Your code is fine:

const int GUESSES = 10;
int guess[GUESSES];

>> I'm thinking that the following code should be placed early on the program

Yes

>> perhaps the beginning of the main function?

>> Question 1: yes, I would rather it be a global variable.

If it's global, it needs to be placed BEFORE all functions.


>> Question 3: not sure about this question.

Don't worry about this question (or the others. They are all answered at this point). You don't need to initialize anything or worry about it if you do the rest of the program right. Give my 5 steps a try.

I changed a few things that you mentioned. I started to write an IF loop. I don't know how to incorporate the userGuess variable to the array.

#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.

// testing adding an array
const int GUESSES = 10;
int guess[GUESSES];
int numberTries;

// end of array testing

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 ()
{
	srand(time(NULL)); // Part one of random number generator.

	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
		
		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;
			for (numberTries = 0; numberTries < GUESSES; numberTries++)
			{
				if ()
				{
				}
			}

			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;
}

I changed my code again, and seem to be much closer now. The program is compiling fine. The output is producing "0" for each guess instead of what each guess actually is. Also, the program is continuing to ask for more guesses even after getting the correct number.

Here is my updated 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 ()
{
	srand(time(NULL)); // Part one of random number generator.

	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 MaxGuesses = 10;
		int NumTries = 0;
		int Guesses[MaxGuesses];
		
		
		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
		
		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)
		{
			int userGuess = 0;
			Guesses[NumTries] = userGuess;
			NumTries++;

			cout << "Please enter your guess : " ; // output
			cin >> userGuess;  // Input - User to enter a guess between 1 and 100
			for (int i = 0; i < NumTries; i++)
			{
				cout << "Guess: " << Guesses[i] << "\n";
			}
			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;
}

numberTries gets incremented every time the user makes a bad guess. Hence it can't be incremented in a loop that does not involve the user making a guess, so lines 61 - 66 are a problem, regardless of what you have in your if statement (and do you even NEED an if statement?)

What is the intent of lines 61 to 66 anyway? Is this where you display the bad guesses? If so, numberTries should not change, which means you can't have numberTries++ on line 61.

If you go back to my list of 5 steps, provided the user guesses wrong 10 times, steps 2 through 5 will be repeated 10 times. Hence your while loop would be steps 2 through 5.

Add a comment before line 61. What is the goal of that for-loop?

As far as your revised code, look at lines 54 and 55.

int userGuess = 0;
Guesses[NumTries] = userGuess;

You should not be surprised you are getting all zeroes! :)

Line 55 either needs to change or be placed somewhere else.

VernonDozier, I truly appreciate your help. I made the FOR loop in 60-65 to output the guesses that have been made, but all it is outputting is a 0 for every guess. I'm stumped with roughly 3 hours to get this done and get some sleep before the beginning of a long day at work deployed in SouthEastAsia. I'm almost to the point of turning this in incomplete, which I would hate to do but it's better than not turning it in at all.

I think that line 55 removal did the trick. Here is my updated code, the only thing left to do I believe is to somehow put in an output message if the user gets to the max tries without success.

#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 ()
{
	srand(time(NULL)); // Part one of random number generator.

	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 MaxGuesses = 10;
		int NumTries = 0;
		int Guesses[MaxGuesses];
		
		
		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
		
		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)
		{
			
			Guesses[NumTries] = userGuess;
			NumTries++;

			cout << "Please enter your guess : " ; // output
			cin >> userGuess;  // Input - User to enter a guess between 1 and 100
			
			// FOR statement used to list the guesses made by the user
			for (int i = 0; i < NumTries; i++)
			{
				cout << "Guess: " << Guesses[i] << "\n";
			}
			
			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;
}

Also, the program is still not limited to ten tries. What do I gotta do to fix that?

Thanks!

What's the maximum number of times the user can guess? What's the maximum number of times the user should go through the while loop? Are these numbers related? Forget the computer code. Pretend there are two kids playing this game and you are the referee. What do you check before saying "Game Over"? What are the criteria for going through the "loop" of one kid guessing and the other checking? Now turn that criteria into computer code and stick it where it belongs (in line 52, the while loop). The game is over if the guess is correct. What else can cause the game to be over? Stick that condition in the while loop.

while((userGuess != secretRandom) && (/*what goes here?*/))

while((userGuess != secretRandom) && (/*what goes here?*/))

That makes sense! Now, how do I word it. (Guesses <= MaxGuesses) ??

And then there is the matter of putting in a output message tht tells the user they lost..not sure where to place that.

the program is still not limited to ten tries. What do I gotta do to fix that?

See last post. As to where to place the message saying "You lose" after ten tries, again, you're the ref. Forget the code and pretend you were writing directions in English. It has to go AFTER the last try. Also, presumably you would say "Sorry. You lose. Do you want to play again?" rather than "Do you want to play again? Sorry. You lose." Now turn the order that you say things into the order that the computer executes the code.

I have to go to sleep myself now, so good luck!

>> (Guesses <= MaxGuesses)

Remember your variable NumTries? That should be in there somewhere, right? otherwise, what's the point of the variable? Guesses[] is the array, so you can't compare an array to a number.

>> And then there is the matter of putting in a output message tht tells the user they lost..not sure where to place that.

See last post. You are no longer repeating anything so it needs to be AFTER that.

And look at lines 55 and 56 again. Think about your FIRST trip through the loop. No guesses have been made yet, but you are already storing a guess? The code is correct. The placement is incorrect. You can't increment the number of tries until a try has been attempted. You cannot store a guess until a guess has been made.

Edited 5 Years Ago by VernonDozier: Changed 54 &amp; 55 to 55 &amp; 56.

VernonDozier, thank you!! It is all good to go, I truly appreciate your expertise, and especially the fact that you didn't just give me the answer. Here is my final 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.
#include <string> // Header used to allow string usage.

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 ()
{
	srand(time(NULL)); // Part one of random number generator.

	bool playAgain=true;  // Boolean expression used to offer the ability to play the game again.
	while(playAgain)
	{
		int secretRandom = 0; // randomly generated number
		int userGuess = 0; // users guess of the random number
		const int MaxGuesses = 10; // maximum number of  user guesses allowed
		int NumTries = 0; // count of attempts at guessing the random number
		int Guesses[MaxGuesses]; // array
		
		
		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
		
		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) && (NumTries < 10))
		{
			
			

			cout << "Please enter your guess : " ; // output
			cin >> userGuess;  // Input - User to enter a guess between 1 and 100

			Guesses[NumTries] = userGuess;
			NumTries++;
			
			// FOR statement used to list the guesses made by the user
			for (int i = 0; i < NumTries; i++)
			{
				cout << "Guess: " << Guesses[i] << "\n";
			}
			
		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 outputs a message if the user fails to get the random number
		if ((NumTries = 10) && (userGuess != secretRandom))
		{
		cout << "You have attempted the maximum number of times! :(" << 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; 
		}
			
	}
	string 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;
}
This question has already been answered. Start a new discussion instead.