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
void PrintHeading (); //prints output heading
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
PrintHeading (); //Prints output heading
//PlayGame (money);
//if (again = true)
bet = GetBet (money, bet);
GetGuess ();
CalcNewMoney (money, bet, guesses);
}
void PrintHeading ()
{
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;
}
}