C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-24-2009, 08:36 PM   #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
Quote:
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!
jualin is offline   Reply With Quote
Old 10-24-2009, 08:51 PM   #2
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> 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?
Sebastiani is offline   Reply With Quote
Old 10-24-2009, 09:09 PM   #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.
jualin is offline   Reply With Quote
Old 10-25-2009, 08:18 PM   #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. lordtalonhands@gmail.com

Hope I can help you out! Feel free to contact me.
-Ryan

Last edited by ryandamartini; 10-25-2009 at 08:21 PM.
ryandamartini is offline   Reply With Quote
Old 10-25-2009, 09:02 PM   #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. lordtalonhands@gmail.com

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.
jualin is offline   Reply With Quote
Old 10-25-2009, 09:24 PM   #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.
ryandamartini is offline   Reply With Quote
Reply

Tags
basic, functions, game, hilo

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ace high and low in card game ninety3gd C Programming 3 05-10-2009 08:16 PM
C/C++, low or high level? Sentral General Discussions 4 01-23-2007 11:43 PM
what is the significance of low order and high order bits Shadow12345 Windows Programming 1 11-16-2002 11:46 AM
high or low ! Beginner2002 C Programming 3 07-29-2002 01:24 PM
low value, high value and increment Unregistered C Programming 2 11-25-2001 09:01 AM


All times are GMT -6. The time now is 12:00 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22