-1

This program is a high-low guessing game where a random number is generated and the user has 6 attempts to guess the number. I only copied my main function and the definition of the DrawNum and GetGuess functions, will post more if asked/needed. My goal is to have the DrawNum function return the random number and call the DrawNum function in the GetGuess function (if that is even the most efficient way of doing it). The function builds fine, but when I run the program I get Run-Time Check Failure #3 - the variable 'MaxNum' is being used without being initialized.

int main ()
{
    int money;
    int bet;
    int guesses;
    unsigned int seed = 0;

    srand(static_cast<unsigned>(time(NULL)));   //get a value for the time from the     computer's clock and with
    srand (seed);                               //it calls     srand to initialize "seed" for the rand() function

    PrintHeading ();                            //Prints output heading
    GetBet (money, bet);
    GetGuess ();
    CalcNewMoney (money, bet, guesses);
    bool PlayAgain ();

}
int DrawNum (int max)
{
    double x = RAND_MAX + 1.0;      /* x and y are both auxiliary */
    int y;                          /* variables used to    do the */
                                    /* calculation */

    y = static_cast<int> (1 + rand() * (max / x));
    return (y);                     /* y contains the result */
}

int GetGuess ()
{
    int guess;          //user's guess
    int guesses;    //number of Guesses
    int MaxNum;
    int RandNum;

    RandNum = DrawNum (MaxNum);

    for (int guesses = 1; guesses <= 6; guesses++)
    {
        cout << "Guess " << guesses <<  ":" << endl;
        cin >> guess;
        if (guess > RandNum)
        {
            cout << "Too high... " <<endl;
        }
        else if (guess == RandNum)
        {
            cout << "Correct!" << endl;
        }
        else
        {
            cout << "Too low... " << endl; 
        }
    }
    return (guesses);
}
user3470962
  • 19
  • 1
  • 5
  • 4
    So what is your question? Is it not working? Are you getting an error? – Cory Kramer Mar 28 '14 at 12:17
  • Please [use ``](http://stackoverflow.com/a/20136256/493122). – Shoe Mar 28 '14 at 12:21
  • Edited my post, my error is "Run-Time Check Failure #3 - the variable 'MaxNum' is being used without being initialized." – user3470962 Mar 28 '14 at 12:33
  • @user3470962 did you read the error message? You are passing `MaxNum` into `DrawNum()` without ever setting/initializing `MaxNum` to anything... Not that it matters anyway, because `DrawNum()` never actually uses that input for anything. – Cory Kramer Mar 28 '14 at 12:48
  • @Cyber I must use the function prototype int DrawNum (int max); however I dont see why generating a random number would have a parameter – user3470962 Mar 28 '14 at 15:24
  • @user3470962 I would think that it is supposed to act as "Generates a random number between `1` and `MaxNum`" – Cory Kramer Mar 28 '14 at 15:52
  • @Cyber, why is it that when I generate a random number I always get the same number? I changed up my code – user3470962 Mar 28 '14 at 22:37

1 Answers1

1

From the header of the question I got that your main concern is random generation of the number.
If I am right here is my suggestion(otherwise you should reformulate your question, by providing additional info about that exactly do you need):
Your random generator isn't correct, random numbers generated by rand() aren't distributed uniformly it is well known fact, and using % - results in that first numbers in sequence are lucky to be chosen.

So I suggest you to use random_engine generator or random_device

std::default_random_engine generator(time(0));
 std::uniform_real_distribution<double> distribution(first, last);
 return distribution(generator);

disadvantages: if open multiply programs which use the same random number generator they will output the same results, because they have the same value of seed which is time. This issue solved by using random device, see description beyond:

std::uniform_real_distribution<double> distribution(first, last);
 std::random_device rd;
 std::default_random_engine generator( rd() );
 return distribution(generator);
spin_eight
  • 3,925
  • 10
  • 39
  • 61
  • I must use the function prototype int DrawNum (int max); however I dont see why generating a random number would have a parameter – user3470962 Mar 28 '14 at 15:23