Hello Everyone,

I have a random number game program that I need to change to use a Class with a constructor. This is my first attempt at using these things, and the instructor hasn't been much help. I watched all of antiRTFM's videos up to video #37, but using constructors and classes correctly is eluding my comprehension. Here is what I have so far:

#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:
	int secretRandom()
	return randomNumber;
private:
	int randomNumber;
	int randomNumber(randomNumber <= 100, >= 1);
}

int main()
{
	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

	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; // initializing variable to 0
		int userGuess = 0; // initializing variable to 0
				
		const int MAX = 100, MIN = 0;  // Sets limits on the random number generator and the range of guesses allowed by the user.

		secretRandom = (rand() % (MAX - MIN + 1)) + MIN; // Part two of random number generator.  This randomly generates the number.

		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 > MAX || userGuess < MIN)
			{
				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;
}

My instructions are that I need to create a class using the name GuessingGame. In that class, i need to use a nonstatic member variable integer. I need to store the random number that is being generated here. The random number generator needs to be obtained from a built in function within the class. There needs to be a static member constant for the maximum number the random number generator can create. There needs to be a constructor that will set the initial random number value. This constructor needs to be used to reference the random number. Everything else needs to be in the Main function. I appreciate any guidance I can get with this. Thanks!

This constructor needs to be used to reference the random number.

This doesn't make sense. Constructors are called on the initial creation of the object. You can't "call" the constructor to have it perform a function (That is what the class's methods are for) In order to get the random number in the object, create a method that returns the variable. Something like this:

class GuessingGame
{
   int m_RanNum;  // To hold the random number

   public:

   // Insert code for the constructor and other methods here

   int getRandomNum()
   { return m_RanNum; }
};

There's nothing to be scared of when it comes to constructors, they're actually very helpful. Your class declaration currently looks like this:

class GuessingGame 
{
public:
	int secretRandom()
	return randomNumber;
private:
	int randomNumber;
	int randomNumber(randomNumber <= 100, >= 1);
}

In this case, you haven't specified a constructor. This doesn't cause a compilation problem because the compiler will use a default one for you (although there are several other problems with it that will, but we'll ignore those for now). In your case, this isn't helpful, since you want to do things with the constructor (set some values etc). To make a constructor for your class, just do this:

class GuessingGame 
{
public:
    GuessingGame();    /* This is the constructor */
}

This class doesn't have any variables yet though, so we can add one now:

class GuessingGame{
public :
    GuessingGame();

private :
    int number;
};

OK, so that's great, but we need to use the constructor to set the number to the value that we want. The aim of the game is to be able to set the number by doing something like this in your program:

int main()
{
    int myNumber = 6;
    GuessingGame newGame(myNumber);

    return 0;
}

So, we make the constructor look like this:

class GuessingGame{
public :
    GuessingGame(int startNumber);

private :
    int number;
    int anotherNumber;
};

/* Here is the definition of the constructor */
GuessingGame::GuessingGame(int startNumber){
    number = startNumber;  // This variable is set with the value passed to the constructor
    anotherNumber = -10;   // This one is set to a default value, but you could set it with, say, rand()...
}

This doesn't do exactly what you need for your project, but it should help you see how you can use the constructor to set values of member variables :)

I realize it's far off. What I posted was kindof a work in progress. It worked fine in the form I start this week with, but I'm having trouble converting it to an OOP using classes and constructors and whatnot. I have been messing with the class a little and changed it to this:

class GuessingGame 
{
public:
	int random()
	{
		srand(time(NULL)); // Part one of random number generator.
		secretRandom = (rand() % (maxRandomNumber - minRandomNumber + 1)) + minRandomNumber; // Part two of random number generator.  This randomly generates the number.
	}
private:
	int secretRandom;
	int storeRandomNumber;
	static const int maxRandomNumber = 100;
	int minRandomNumber;
}

Does it seem like I am in the right direction?

BTW, I did this before reading any replies, which I will do after posting this...thanks!

You can't "call" the constructor to have it perform a function

Er, yes you can. See the example that I just gave. The constructor gets called when you create the variable, but other than that, you can do most things with it that you can do with any other member function.

I'm confused on how to do the constructor, I think I'm intimidated.

#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:
	int random()
	{
		srand(time(NULL)); // Part one of random number generator.
		secretRandom = (rand() % (maxRandomNumber - minRandomNumber + 1)) + minRandomNumber; // Part two of random number generator.  This randomly generates the number.
	}
	GuessingGame();

private:
	int secretRandom;
	int storeRandomNumber;
	static const int maxRandomNumber = 100;
	int minRandomNumber;
}

int main()
{
	int secretRandom; // initializing variable
	int userGuess = 0; // initializing variable to 0

	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(secretRandom);
	
	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 > MAX || userGuess < MIN)
			{
				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;
}
class GuessingGame 
{
public:
	int random()
	{
		srand(time(NULL)); // Part one of random number generator.
		secretRandom = (rand() % (maxRandomNumber - minRandomNumber + 1)) + minRandomNumber; // Part two of random number generator.  This randomly generates the number.
	}
private:
	int secretRandom;
	int storeRandomNumber;
	static const int maxRandomNumber = 100;
	int minRandomNumber;
}

Does it seem like I am in the right direction?

It's getting there, I think you're still a little confused though. In addition to understanding the C++, you should also take some time to think about how to construct your class. I'd start by thinking about the things that you want the class to do, so a non-exhaustive list might be

  • Initialise itself to a random number.
  • Have a maximum and minimum range for the maximum number (the min might be zero)
  • Check user-supplied numbers to see if they match the stored random number
  • Maybe reset the random number to a new one

Now, you have to decide which of these is a function that needs performing and which is a value that needs remembering. These will form the methods and variables, respectively. If you start with the variables, then you have

  • currentRandomNumber
  • maxRandomNumber
  • minRandomNumber

When a variable is declared static then there's only one copy of the variable and it's shared between all the classes of the same name. So this might be a good thing, if you wanted to, say, specify a global maximum that any random number in any of the GuessingGames that you have declared could take. I'll leave it to you to think about which of the above features might best be described by functions.

I would anticipate that you would use your GuessinGame class something like this:

int main(){
    /* Make a GuessingGame object for the game, using the constructor to initialise */
    int seed = 0;
    GuessingGame currentGame(seed);    // seed could be the seed to the random number generator for the initial random number

    while(1){
        int userGuess;
        std::cout << "Enter a guess [0 - 100]: ";
        std::cin >> userGuess;
        
        if( currentGame.check(userGuess) == true ){
            std::cout << "Yay!" << std::endl;
            char c;
            std::cout << "Play again?";
            std::cin >> c;
            if( c == 'y' ){
                currentGame.reset();
            }
            else{
                break;
            }
        }
    }
    
}

To see how you can set values in the constructor, have a look at the example I posted previously and see if you can adapt it to do what you want. Another clue is that you can't set the maximum number in the way that you have, i.e.

class GuessingGame{
public:

private:
	static const int maxRandomNumber = 100;  // You can't do this!
}

You have to do this in the constructor.

Wow, your last post really confuses me. I think I'm starting to see double, it's midnight here where I am deployed. I gotta have this done in six hours, and then once it is done, I need to change it again and finish that in like 3 hours.

Wow, your last post really confuses me.

Really? Which part? (Don't say "everything". If it's all confusing then just pick the bit you're most confused about and we might be able to go from there).

OK, so let me try to break it down. The headers I'm using seem to be fine.

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

Next, I have a class:

class GuessingGame 
{
public:
	int random()
	{
		srand(time(NULL)); // Part one of random number generator.
		secretRandom = (rand() % (maxRandomNumber - minRandomNumber + 1)) + minRandomNumber; // Part two of random number generator.  This randomly generates the number.
	}
	GuessingGame();

private:
	int secretRandom;
	int storeRandomNumber;
	static const int maxRandomNumber = 100;
	int minRandomNumber;
};

This needs to be changed. I believe the class needs to have 3 different variables. #1 - a nonstatic member variable integer that will be used to store the random
number.
#2 - a static member constant which will be used to store the maximum possible
number the generator can generate, which should be 100.
#3 - an initial random number value (needs to be in the constructor).

The main function is last:

int main()
{
	int secretRandom; // initializing variable
	int userGuess = 0; // initializing variable to 0

	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(secretRandom);
	
	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 > MAX || userGuess < MIN)
			{
				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;
}

I don't think this is right either, but I don't know where to begin to fix it.

I'm confused on whether the random number generator itself should be in the Class or in the Main function.
I'm confused on how to make the class and the constructor within the class, correctly.
I'm confused on how I use the Class in the main function, once it is correct.

Wow, your last post really confuses me. I think I'm starting to see double, it's midnight here where I am deployed. I gotta have this done in six hours, and then once it is done, I need to change it again and finish that in like 3 hours.

It's a lot to digest in less than an hour. You'll probably need to read it several times, then play around with experimenting with it. Classes are a major part of C++, but you can't learn them too quickly.

Six hours? Three hours? If it's a brand new concept, that's pushing it. You can probably pull something off in that time that works, but I wouldn't expect to grasp all the fine points (i.e. when to use static and when not to use static variables, etc. They're important to know, but in your timeframe, you might want to google "C++ class static variables" and just try to get the syntax down first).

I know what you mean Vernon about not being able to grasp it without proper practice. I've been trying to grasp this stuff for a few weeks but haven't had much luck. My instructor has done less than instruct, so I've been watching the YouTube videos antiRTFM posted on C++. I've watched videos 30-37 like 5 times each, and it seems like it makes sense when I watch them, but then I try to implement the ideas in my program without success. When I go to do what he is doing in the videos, I feel like its a foreign language that I'm stumbling through.

I'm gonna start from scratch with the class code.

class GuessingGame()
{
     public:

          need a function here to store the randomly generated number
          
          constructor goes here to set initial value of random number.  The range of
          possible random numbers needs to be between 1 and 100, so should this be set
          to 1?  Don't know how to write this properly.

     private:
          
          need a static member constant to set the maximum number of 100

          do I initialize an integer here to be used in the function that stores the
          random number?

          do I need any other items here?
class GuessingGame 
{
public:

	GuessingGame()
	{
		int maxRandomNumber = 100;
		int minRandomNumber = 1;
		GuessingGame.random = (<= maxRandomNumber) && (>= minRandomNumber);
	}

	int random()
	{
		srand(time(NULL)); // Part one of random number generator.
		secretRandom = (rand() % (maxRandomNumber - minRandomNumber + 1)) + minRandomNumber; // Part two of random number generator.  This randomly generates the number.
	}
	
private:
	int secretRandom;
	int storeRandomNumber;
	int maxRandomNumber;
	int minRandomNumber;
};

I'm still not sure if the random number generator belongs in the class or in the main function.

I'm confused on whether the random number generator itself should be in the Class or in the Main function.

I think you're making a big deal of the random number generator in your head, and it's not. Just think of it as a function that you can call to set a variable to a number. I'll get to this below.

I'm confused on how to make the class and the constructor within the class, correctly.

Ok, So you have something like:

class GuessingGame()
{
     public:

          need a function here to store the randomly generated number

          constructor goes here to set initial value of random number.  The range of
          possible random numbers needs to be between 1 and 100, so should this be set
          to 1?  Don't know how to write this properly.

     private:

          need a static member constant to set the maximum number of 100

          do I initialize an integer here to be used in the function that stores the
          random number?

          do I need any other items here?
};

To answer the questions that you have, a class has two kinds of thing in it: member functions and member variables. The functions do things and the variables remember things. Remembering this, you can't store anything in a function, you need to use a variable to do this.

An example of a constructor that gives values to variables could be:

class MyClass{
public :
    MyClass(){   m_variable = 10;   }

private :
    int m_variable;
}

So, for this class, when you instantiate an object in your code, like this:

int main(){
    MyClass a;
    return 0;
}

then m_variable gets set to 10. If you wanted it set to a random number then you could use

srand(0);
m_variable = rand();

instead. You could also set the max and min variables in the same way (also in the constructor, there's no limit on the amount of stuff that you can do in it really)

You can't initialise any variables in the class declaration, you have to do that in the constructor as for m_variable above.

I'm confused on how I use the Class in the main function, once it is correct.

You just use it like any other type, such as int, as in the above example.

If you want to use the member functions, then you should use the dot operator to get at them, like this:

class MyClass{
public :
    MyClass(int seed){
        srand(seed);
        m_variable = rand();
    }
    int getVariable(){
        return m_variable;
    }

    void regenerate(){
        m_variable = rand();
    }

private :
    int m_variable;
};

int main{
    MyClass myObject(0);             // Instantiate an object of type "MyClass"
    int x = myObject.getVariable();  // Get the value of the variable
    std::cout << x << std::endl;     // Output variable

    myObject.regenerate();           // Make a new random number
    std::cout << myObject.getVariable() << std::endl;    // Output the number directly

    return 0;
}

Hope that helps some.

The random number generator is a function, ok I get that. It belongs in the public portion of the class right?

The private portion of the class will be used to initialize any variables I may be using in the class right?

I did a few changes, and now its compiling without errors, but it still doesn't feel like it's right.

#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 secretRandom)
	{
		storeRandomNumber = secretRandom;
		int maxRandomNumber = 100;
		int minRandomNumber = 1;
	}
		
	
private:
	int storeRandomNumber;
	int maxRandomNumber;
	int minRandomNumber;
};

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 = 0;  // 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

		GuessingGame newGame(secretRandom);

		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 > MAX || userGuess < MIN)
			{
				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;
}
}

Er, yes you can. See the example that I just gave. The constructor gets called when you create the variable, but other than that, you can do most things with it that you can do with any other member function.

I think you misunderstood my post. The OP says he wants to reference the member variable by calling the constructor (i.e get the value of the variable). You cannot do that (The constructor cannot have a return type). In order to reference a member variable you need to create a method to return the value.

After reviewing whaat I did a bit ago, I realized the class wasn't doing anything the way I had it, so I changed a bunch of stuff, and now it's not working again LOL. OMG this is frustrating.

#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 secretRandom)
	{
		storeRandomNumber = secretRandom;
		int maxRandomNumber = 100;
		int minRandomNumber = 1;
	}
	
	int storeRandomNumber()
	{
		srand(time(NULL)); // Part one of random number generator.
		storeRandomNumber = (rand() % (maxRandomNumber - minRandomNumber + 1)) + minRandomNumber; // Part two of random number generator.
	}
	
private:
	int storeRandomNumber;
	int maxRandomNumber;
	int minRandomNumber;
};

int main ()
{
	

	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
				
		// 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();
		GuessingGame.storeRandomNumber();

		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 > GuessingGame.maxRandomNumber || userGuess < GuessingGame.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;
}
}

First get the static/maximum number part out of the way. It's a couple of line changes and that's it:

Change ravenous's last post slightly:

class MyClass{
public :
    MyClass(int seed){
        srand(seed);
        m_variable = rand() % (MAX + 1);
    }
    int getVariable(){
        return m_variable;
    }
 
    void regenerate(){
        m_variable = rand() % (MAX + 1);
    }
 
private :
    static const int MAX = 100;
    int m_variable;
};

Done. No more "static" variable issues. That's all there is to it. It's one of those things that's just easier to show you. You can now get then word "static" out of the picture if I am reading the problem correctly.

I think my last iteration will work if I can figure out how to fix why lines 49 and 62 are giving me inaccessible errors.

Lines 27 and 28 of your code:

int maxRandomNumber;
	int minRandomNumber;

These are static. I didn't realize you had a minimum. Fine. My code changes to this. Change "MyClass" to "GuessingGame":

class MyClass{
public :
    MyClass(int seed){
        srand(seed);
        m_variable = rand() % (MAX - MIN + 1) + MIN;
    }
    int getVariable(){
        return m_variable;
    }
 
    void regenerate(){
        m_variable = rand() % (MAX - MIN + 1) + MIN;
    }
 
private :
    static const int MAX = 100;
    static const int MIN = 1;
    int m_variable;
};

Your constructor is way off. Use ravenous's (or use my changes to his). The constructor needs to take the SEED. And you do not set the MAX and MIN. They're static, they're constant, they're set at compile time outside of any function, as shown.

My guess is that you'd call the constructor from main with this:

GuessingGame gg(time(0));

Line 48 - no parentheses.
Lines 49 - Bad function call:

GuessingGame newGame();
GuessingGame.storeRandomNumber();

Should be this.

GuessingGame newGame;
newGame.storeRandomNumber();

But really you should be calling this constructor only once as far as I can tell. Don't call it inside of a loop.

I made the changes you suggested, but am apparently doing something wrong.

#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;
		newGame.storeRandomNumber();

		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 > GuessingGame.maxRandomNumber || userGuess < GuessingGame.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;
}
}

where does "seed" come from? and where do I use "regenerate". Do I need to change the main function? Gah, I felt like I was starting to understand earlier, but now I feel even more confused.

where does "seed" come from? and where do I use "regenerate". Do I need to change the main function? Gah, I felt like I was starting to understand earlier, but now I feel even more confused.

Wooah take a breath real quick haha. When you call srand() you need to pass it a "seed" to generate the random number. Typically you pass it the time() function with null as the argument to time():

srand(time(NULL));

You should be getting a complier error because the default constructor no longer exists. You declared your own constructor and it HAS to take one argument when you declare it. This will not work anymore:

GuessingGame game;  // ERROR: the constructor needs one argument of type int

When you create a new object you want to pass the seed to the constructor:

// Declare a new GuessingGame object
GuessingGame game(time(NULL));  // Pass time() function here

You would then want to call the regenerate() method on your object ( in main() ):

game.regenerate();

Ok, I changed the class to:

class GuessingGame 
{
public:

	GuessingGame(int seed)
	{
		srand(time(NULL));
		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;
};

I also added the time call to the constructor in the main function. I'm getting a new error now telling me the regenerate call in the main function is wrong.

My last comment on the two lines only addressed the syntax. The way you had it, it wouldn't work no matter what. Changing them to my last changes would work if you had a NULL constructor. Since you don't, it didn't. You've been going back and forth between NULL and non-NULL constructors, so I didn't point that out. See hag++'s post above.

In your constructor you should be passing the variable "seed" to the srand() function because if you create the object correctly in main() then the "seed" variable will have the value of time(NULL) in it. Like this:

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

Also, can you post the code in main() so we can see how you are calling regenerate()?

Oh and this is also incorrect:

if(userGuess = secretRandom)

It needs to be

if(userGuess == secretRandom)
#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;
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.