# Thread: High and low game

1. ## High and low game

The purpose of this project is make use function prototypes to achieve the main objective of the program. This main objective is for the computer to generate a random number between 1 - 100 inclusive. The user is given \$1000 to start, then the user will bet some money and needs to guess the right number in a certain number of tries. If the user wins then he wins the bet, if he doesn't then he loses the bet. At the end of the program a function called "playagain" will ask the user if he wants to play again, if the user answers yes, then the money (lost or won) will be utilized for the 2nd game.
The issue that I am having is to be able to tell the user how much percentage of the games he has won at the end of each game.
My T.A. told me
that every time the function playgame is called, that counts as one game, so I could keep track of that in the do while in main. He also said that I can determine the wins by comparing the returned by playgame with the money from before you called playgame (with another money variable)...if the new money is more, you know you won.
This is part of my code in main. I am not allowed to change the parameters of any of the functions, even though I thought about doing that.

Code:
```/* MAIN FUNCTION */
int main()
{

/* VARIABLES */
int money = 1000;           // money user starts with
double numwins = 0,      // number of won games
numgames = 0;  // number of games played
/*Print an introductory title*/
srand(static_cast<unsigned>(time(NULL)));

do
{
money = PlayGame(money);   // PlayGame should return the money at the end of the game

numgames++;                  // a new game will be counted each time Playgame runs

} while ( PlayAgain() && newmoney > 0 );         // user can play again if he says "yes" in PlayAgain and if he has money
cout << "You have won " << setprecision(4) << (numwins/numgames) * 100 << "% of the games you played" <<endl;```
This code is not working correctly, any ideas?
Thank you!

2. >> The issue that I am having is to be able to tell the user how much percentage of the games he has won at the end of each game.

How can you calculate the ratio if 'numwins' never get's updated?

>> I am not allowed to change the parameters of any of the functions, even though I thought about doing that.

In that case, make 'numwins' a global variable. Also, I'm assuming 'newmoney' is being modified inside the 'PlayGame' function - correct?

3. ## Re:

My professor does not allow me to use global variables, thank you for the idea. Also newmoney is not being modified inside of the PlayGame function because the function returns the integer "money", and I cannot change the parameter of the function.

This is my new improved code which almost works, the only problem is that the percentage is off by one game won. For instance if I won 4 games out of 5 it tells me that I won 60% instead of 80%.
Code:
```int main()
{
/* CONSTANTS */

/* VARIABLES */
int money = 1000,
oldmoney = money;
double 	numgames =0,
numwins=0;

/*Print an introductory title*/
srand(static_cast<unsigned>(time(NULL)));

do
{
money = PlayGame(money);

numgames++;
if (money > oldmoney)
{
numwins++;
}
else
{
numwins = numwins;
}
} while ( PlayAgain() && money > 0 );
cout << "You have won " << setprecision(4) << (numwins/numgames) * 100 << "% of the games you played" <<endl;

return (0);
}```
Thank you for the quick response.

4. I lurk here for searching through the forums and ran across your thread. I think I should be able to help you. Is it possible for you to post the code as a whole? You can also contact me through aim: winggundam3451 Just hard to fix everything without being able to load it into VS. Fastest way would be to send me a msg to my gmail. lordtalonhands@gmail.com

-Ryan

5. Originally Posted by ryandamartini
I lurk here for searching through the forums and ran across your thread. I think I should be able to help you. Is it possible for you to post the code as a whole? You can also contact me through aim: winggundam3451 Just hard to fix everything without being able to load it into VS. Fastest way would be to send me a msg to my gmail. lordtalonhands@gmail.com