Hi, this program is a high-low guessing game that generates a random number and the user has 6 guesses to win their bet. When I run my code the "random number" is always the same. Any help is greatly appreciated.

``````#include <iostream>
#include <iomanip>
#include <ctime>         /* to access the computer's clock */
#include <cstdlib>           /* needed for rand() and RAND_MAX */

using namespace std;

//function prototypes
int DrawNum (int max);                                              //draws a random number
int GetBet (int money, int& bet);                                   //gets users bet
int GetGuess ();                                                    //gets the users guess
int CalcNewMoney (int money, int bet, int guesses);                 //calculates amount of money user has after the round
bool PlayAgain ();                                                  //asks user to play again
int PlayGame (int money);

int main ()
{
int money = 1000;
int bet;
int guesses;
unsigned int seed = 0;
//bool again = PlayAgain ();

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

//PlayGame (money);
//if (again = true)
bet = GetBet (money, bet);
GetGuess ();
CalcNewMoney (money, bet, guesses);

}

{

cout << "========================================================" << endl;
cout << "Welcome to the high-low betting game!" << endl;
cout << "You have \$1000 to begin the game. " << endl;
cout << "Valid guesses are numbers between 1 and 100. " << endl;
cout << "========================================================" << endl << endl;
return;
}

/*int PlayGame (int money)
{

}*/

int GetBet (int money, int& bet)
{

cout << "Please enter a bet: " << endl;
cin >> bet;

if (bet < 1 || bet > money)
{
cout << "This isn't a valid bet!" << endl;
cin >> bet;
}
return (bet);
}

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 = 1;        //number of Guesses
int MIN = 1;
int MAX = 100;
int RandNum;

RandNum = DrawNum (MAX);

for (int guesses = 1; guesses <= 6; guesses++)
{
cout << "Guess " << guesses <<  ":";
cin >> guess;
if (guess > RandNum)
{
cout << "Too high... " <<endl;
}
else if (guess == RandNum)
{
cout << "Correct!" << endl;
break;
}
else
{
cout << "Too low... " << endl;
}
}
return (guesses);
}

int CalcNewMoney (int money, int bet, int guesses)
{
int wins = 0;
int games = 0;
int RandNum;
bool win = false;
guesses = GetGuess();

for (int guesses = 0; guesses <= 6; guesses++)
{
if (win)
{
wins++;
games++;
cout << "You just won \$" << bet/guesses << endl;
cout << "You have \$" << money + (bet/guesses) << endl;
cout << "You have won " << (wins*100)/games << "% of the games you played" << endl;
return (money + (bet/guesses));
}
else
{
games++;
cout << "Sorry... the correct answer was " << RandNum << endl;
cout << "You lost \$" << bet << endl;
cout << "You have \$" << money - bet << endl;
cout << "You have won " << (wins*100)/games << "% of the games you played" << endl;
return (money - bet);
}
}
}

bool PlayAgain ()
{
char Again;
cout << "Play again? (y/n)" << endl;
cin >> Again;
if (Again == 'y' || Again == 'Y')
{
return true;
}
else
{
cout << "Thanks for playing." << endl;
return false;
}
}
``````

delete line 26 because it destroyed the seed generated by line 25.

## All 5 Replies

delete line 26 because it destroyed the seed generated by line 25.

I thought that worked, but now every time i run the program the "random number" increases by 1, so it's still not random.

line 76: `y = static_cast<int> (1 + rand() * (max / x));`

If all you want to do is to generate a random number for the user to guess then why all that math? I'd change the function to just one line --`return rand();`

line 33: variable guessed is used before it has been initialized.

line 111: why is **guesses ** a parameter? It is never used in that function. If you want line 117 to change the value that is declared in main() then you need to pass the variable by reference, not by value.

`int CalcNewMoney (int money, int bet, int* guesses)`

You should not have two different variables with the same variable name -- it causes lots of confusion and bugs. I'd change the loop count to be standard i counter instead of guesses.

line 128: You will most likely get division by 0 errors because the value of guesses is 0 the first time through the loop.

Also the function can return without a return value (what happens after the loop finishes???)

There are a number of issure with various aspects of this coe:

`int GetGuess()`, I assume is there to return the number of guesses that the player took to get the correct number. However, you use the variable guesses twice. This a scoping issue. First, there is the variable `guesses` which is defined at line 83 and is within scope of the whole function, and then their is the variable guesses that is defined on line 90, that is only within the for loop. Those two variables are different.

Thus the return from the function is ALWAYS 1.

Next, the function DrawNum, maybe not how I would have done it BUT it works. You can check that by doing the simple substitution,
`y = static_cast<int> (1 + 0* (max / x));` and
`y = static_cast<int> (1 + RAND_MAX* (max / x));` and you see you get the result 1, and max (as required).

Then there is the issue of calling GetGuess from main [you also call it from CalcNewMoney.

Also why is there a loop in CalcNewMoney. Delete lines 119,120. and
replace with something like:

``````if (guesses!=7)
win=1;
else
win=0;
``````

[Note: that there is a short for to do that in c/C++ `win=(guesses==6) ? 1 : 0` but if you haven't seen it before the code above works fine.

Also note that GetGuess returns 7 if you don't successfully guess.

You also have a common mistake: `cout << "You have won " << (wins*100)/games << "% of the games you played" << endl;` The calculation (wins*100)/games returns an approximate because wins, games and 100 are integers. You know about this because you put it into your random function.

Be a part of the DaniWeb community

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