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!