Thread: High and low game

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Lightbulb 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)));
    
    	PrintHeading(money);                  // a void function that prints the heading page 
    	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. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    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)));
    
    	PrintHeading(money);
    	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. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    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. [email protected]

    Hope I can help you out! Feel free to contact me.
    -Ryan
    Last edited by ryandamartini; 10-25-2009 at 08:21 PM.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Quote Originally Posted by ryandamartini View Post
    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. [email protected]

    Hope I can help you out! Feel free to contact me.
    -Ryan
    Thank you, I was able to figure it out. I was missing a ++ modifier before the money variable which allowed me to have a higher percentage.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    Quote Originally Posted by jualin View Post
    Thank you, I was able to figure it out. I was missing a ++ modifier before the money variable which allowed me to have a higher percentage.
    cool. You wouldnt happen to go to FSU would you ? My friend has this exact program to work on for his class. Would it be possible for him to meet up with you ? Ive tried to help him from aim, but it has not worked too well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ace high and low in card game
    By ninety3gd in forum C Programming
    Replies: 3
    Last Post: 05-10-2009, 08:16 PM
  2. C/C++, low or high level?
    By Sentral in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2007, 11:43 PM
  3. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM
  4. high or low !
    By Beginner2002 in forum C Programming
    Replies: 3
    Last Post: 07-29-2002, 01:24 PM
  5. low value, high value and increment
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 09:01 AM

Tags for this Thread