0

Since regenerate() is a method it needs to have parenthesis.
Change this:

newGame.regenerate;

to this:

newGame.regenerate();
0

Now the only errors I'm getting involve line 59. The error is saying "cannot access private member declared in class 'GuessingGame'"

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

class GuessingGame 
{
public:

	GuessingGame(int seed)
	{
		srand(seed);
		m_variable = rand() % (maxRandomNumber - minRandomNumber + 1) + minRandomNumber; // Part two of random number generator.
	}
	
	int storeRandomNumber()
	{
		return m_variable;		
	}

	void regenerate()
	{		
		m_variable = rand() % (maxRandomNumber - minRandomNumber + 1) + minRandomNumber;
	}
	
private:	
	static const int maxRandomNumber = 100;
	static const int minRandomNumber = 1;
	int m_variable;
};

int main ()
{
	
		int secretRandom = 0; // randomly generated number
		int userGuess = 0; // users guess of the random number
				
		// Create random number
		
		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

		GuessingGame newGame(time(NULL));
		newGame.storeRandomNumber();
		newGame.regenerate();

		bool playAgain=true;  // Boolean expression used to offer the ability to play the game again.
		while(playAgain)
		{
						
		while (userGuess != secretRandom)  // Loops forever, or until user finds the number.
		{
			cout << "Please enter your guess between 1 and 100: " ; // output
			cin >> userGuess;  // Input - User to enter a guess between 1 and 100
			cout << "\n";

			if(userGuess > newGame.maxRandomNumber || userGuess < newGame.minRandomNumber)
			{
				cout << "Please stay within the parameters of guesses between 1 and 100!" << endl;
				continue;
			}
			if(userGuess > secretRandom)
			{
				cout << "Your guess of " << userGuess << " is too high, guess lower" << endl << endl;
				continue;
			}
			if(userGuess < secretRandom)
			{
				cout << "Your guess of " << userGuess << " is too low, guess higher" << endl << endl;
				continue;
			}
			if(userGuess == secretRandom)
			{
				cout << "Congratulations, you are CORRECT with your guess of " << userGuess << " !" << endl << endl;

				char again;
				cout << "Do you want to play again?  Enter y for yes or n for no: " ;
				cin >> again;
				cout << "\n";

				if(again != 'y')
				{
					playAgain = false;
					cout << endl << "Thank you for playing My Number Generator Game! " << endl << endl;
					continue;
				}
				else(again = 'y');
				{
					playAgain = true;
				}
			}
		}
}
	string yourName;
		cout << "Please type your first name and press enter. " ; // output
		cin >> yourName; // Input - User to enter in their first name
			
		return 0;
}
0

I assume I'm getting those errors because minRandomNumber and maxRandomNumber are private. Any suggestions on that?

0

Right that's because you made those variables private in your class. If you want to access them directly (Without using getter, setter methods) then you need to declare them as public.

0

Just to test it, I moved them to public class, and it compiled with no errors. So I attempted to test the functionality, and it does absolutely nothing

0

Should the regenerate call be elsewhere? It's not making sense to me that this isn't working, when it worked earlier, when my program was all wrong.

0

It seems like it could just be an order problem. When I read the following three lines of code, it make me think that the program is creating a random number, and the recreating a random number, then stopping before it does what it is supposed to do.

GuessingGame newGame(time(NULL));
		newGame.storeRandomNumber();
		newGame.regenerate();
0

I adjusted some items on the while loop, and now it works one time through, but isn't restarted when yes is chosen for the playagain

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

class GuessingGame 
{
public:

	GuessingGame(int seed)
	{
		srand(seed);
		m_variable = rand() % (maxRandomNumber - minRandomNumber + 1) + minRandomNumber; // Part two of random number generator.
	}
	
	int storeRandomNumber()
	{
		return m_variable;		
	}

	void regenerate()
	{		
		m_variable = rand() % (maxRandomNumber - minRandomNumber + 1) + minRandomNumber;
	}
	static const int maxRandomNumber = 100;
	static const int minRandomNumber = 1;
	int m_variable;

};

int main ()
{
	
		int secretRandom = 0; // randomly generated number
		int userGuess = 0; // users guess of the random number
				
		// Create random number
		
		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

		GuessingGame newGame(time(NULL));
		newGame.storeRandomNumber();
		newGame.regenerate();

		bool playAgain=true;  // Boolean expression used to offer the ability to play the game again.
		while(playAgain)
		{
						
		while (userGuess != newGame.m_variable)  // Loops forever, or until user finds the number.
		{
			cout << "Please enter your guess between 1 and 100: " ; // output
			cin >> userGuess;  // Input - User to enter a guess between 1 and 100
			cout << "\n";

			if(userGuess > newGame.maxRandomNumber || userGuess < newGame.minRandomNumber)
			{
				cout << "Please stay within the parameters of guesses between 1 and 100!" << endl;
				continue;
			}
			if(userGuess > newGame.m_variable)
			{
				cout << "Your guess of " << userGuess << " is too high, guess lower" << endl << endl;
				continue;
			}
			if(userGuess < newGame.m_variable)
			{
				cout << "Your guess of " << userGuess << " is too low, guess higher" << endl << endl;
				continue;
			}
			if(userGuess = newGame.m_variable)
			{
				cout << "Congratulations, you are CORRECT with your guess of " << userGuess << " !" << endl << endl;

				char again;
				cout << "Do you want to play again?  Enter y for yes or n for no: " ;
				cin >> again;
				cout << "\n";

				if(again != 'y')
				{
					playAgain = false;
					cout << endl << "Thank you for playing My Number Generator Game! " << endl << endl;
					continue;
				}
				else(again = 'y');
				{
					playAgain = true;
				}
			}
		}
}
	string yourName;
		cout << "Please type your first name and press enter. " ; // output
		cin >> yourName; // Input - User to enter in their first name
			
		return 0;
}
0

Line 36 and any references to it need to go away. The secret number is stored in your class now, which means you need to call storeRandomNumber. By the way, that's not a good name for the function. You aren't storing anything. You're retrieving what's already been stored.

Edited by VernonDozier: n/a

0

I moved the location of the regenerate function, and now it is working properly!!!

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

class GuessingGame 
{
public:

	GuessingGame(int seed)
	{
		srand(seed);
		m_variable = rand() % (maxRandomNumber - minRandomNumber + 1) + minRandomNumber; // Part two of random number generator.
	}
	
	int storeRandomNumber()
	{
		return m_variable;		
	}

	void regenerate()
	{		
		m_variable = rand() % (maxRandomNumber - minRandomNumber + 1) + minRandomNumber;
	}
	static const int maxRandomNumber = 100;
	static const int minRandomNumber = 1;
	int m_variable;

};

int main ()
{
	
		int secretRandom = 0; // randomly generated number
		int userGuess = 0; // users guess of the random number
				
		// Create random number
		
		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

		GuessingGame newGame(time(NULL));
		newGame.storeRandomNumber();
		

		bool playAgain=true;  // Boolean expression used to offer the ability to play the game again.
		while(playAgain)
		{
			newGame.regenerate();

		while (userGuess != newGame.m_variable)  // Loops forever, or until user finds the number.
		{
			cout << "Please enter your guess between 1 and 100: " ; // output
			cin >> userGuess;  // Input - User to enter a guess between 1 and 100
			cout << "\n";

			if(userGuess > newGame.maxRandomNumber || userGuess < newGame.minRandomNumber)
			{
				cout << "Please stay within the parameters of guesses between 1 and 100!" << endl;
				continue;
			}
			if(userGuess > newGame.m_variable)
			{
				cout << "Your guess of " << userGuess << " is too high, guess lower" << endl << endl;
				continue;
			}
			if(userGuess < newGame.m_variable)
			{
				cout << "Your guess of " << userGuess << " is too low, guess higher" << endl << endl;
				continue;
			}
			if(userGuess = newGame.m_variable)
			{
				cout << "Congratulations, you are CORRECT with your guess of " << userGuess << " !" << endl << endl;

				char again;
				cout << "Do you want to play again?  Enter y for yes or n for no: " ;
				cin >> again;
				cout << "\n";

				if(again != 'y')
				{
					playAgain = false;
					cout << endl << "Thank you for playing My Number Generator Game! " << endl << endl;
					continue;
				}
				else(again = 'y');
				{
					playAgain = true;
				}
			}
		}
}
	string yourName;
		cout << "Please type your first name and press enter. " ; // output
		cin >> yourName; // Input - User to enter in their first name
			
		return 0;
}
0

Line 36 and any references to it need to go away. The secret number is stored in your class now, which means you need to call storeRandomNumber. By the way, that's not a good name for the function. You aren't storing anything. You're retrieving what's already been stored.

Thank you for reminding me to get rid of that line. I got rid of it, and I changed the name to storedRandomNumber.

Now to get to work on the update of the program :(

0

Use the object-oriented guessing game program developed in Week Two.

Add functionality to store the guesses the user makes in an array up to a maximum number of guesses stored in a constant. Display the list of guesses after the correct guess has been made. The list will be stored in an array of objects from a new class you create called Guess.

Create three other classes in addition to Guess: High, Low, and Correct. These three will inherit from Guess. The array will use inheritance and polymorphism to display the guess made and a string of whether it was high, low, or correct for each guess. The high, low, or correct string needs to come from a method in these classes. Allow the user to decide if he or she wants to play another game.

0

I created all four of the new classes, which was easy. Now I'm trying to figure out how to do the array and get it in the class. Then I gotta figure out how to use the other classes with their roles.

1

Hi there, it's morning here now. Looks like you did lots of work in the night!

Now that you have your program working, there are some tweeks that you should make, so that it's using its object-oriented features properly.

At the moment you have all public members to your function. This is not typical of object-oriented design in C++. Usually, the member variables will be private. You had this originally, but changed it to remove a compiler error telling you that you can't access a private variable. I think I'd have made your GuessingGame class like this:

class GuessingGame{
public :
    GuessingGame(int seed){
        srand(seed);
        m_RandomNumber = rand()%(m_Max - m_Min + 1) + m_Min;
    }

    int Check(int userGuess) const{
        if(userGuess < m_RandomNumber)
            return -1;
        else if(userGuess > m_RandomNumber)
            return 1;
        else
            return 0;
    }

    void Regenerate(){
        m_RandomNumber = rand();
    }

private :
    int m_RandomNumber;
    static const int m_Max = 100;
    static const int m_Min = 1;
};

If you compare this to your class, you can see that the member variables are all private, which is good, we don't want just anyone being able to get at them and change them!

So, now lets look at how to use this class.

int main(){
    /* Make a GuessingGame object for our game */
    int mySeed = 0;
    GuessingGame game(0);
    /* That's all you need to do to start the game, you don't need the extra calls  */
    /* to your storeRandomNumber and regenerate functions, look at the constructor  */
    /* for GuessingGame, see that it takes the seed that you give it (0 above) and  */
    /* uses it to start the random number generator and then set the m_RandomNumber */
    /* variable to a random number using rand().                                    */

    /* Start the game */
    bool keepGoing;
    do{
        keepGoing = true;
        /* Get a guess from the user */
        int guess;
        std::cout << "Enter a guess: ";
        std::cin >> guess;
        
        /* Check how they did, the Check function allow us to access the private  */
        /* variable m_RandomNumber, without having to worry about the details of  */
        /* how the check if made.  We just give it the user's guess and then we   */
        /* know that the guess is too high, low or correct by the return value of */
        /* the function.  It's neater and we don't need to do the check manually  */
        /* in main().                                                             */
        switch(game.Check(guess)){
        case -1 : std::cout << "Too low! Try again!" << std::endl;
                  break;
        case 1  : std::cout << "Too high! Try again!" << std::endl;
                  break;
        case 0  : std::cout << "Correct!! Would you like another game?" << std::endl;
                  char c;
                  std::cin >> c;
                  /* Regenerate the random number if they want to play again, quit */
                  /* if they don't want to play again.                             */
                  if(c == 'y')
                      game.Regenerate();
                  else
                      keepGoing = false;
                  break;
        }
    }while(keepGoing == true);
}
Votes + Comments
Lots of good help on this thread.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.