Archived

This topic is now archived and is closed to further replies.

Infuscare

Mystery problem

Recommended Posts

I have a program and for some reason, the output of two variables (winnings and bet) is always the same even when i try to change it (through a class) Help...
      
#include "gambleclasses.h"

int bet = 0,wager = 0,winnings = 0;
void main(void)
{
	cout << "Welcome to Gambling 1.0";
	cout << endl << endl;
	
	intro();
	menu();
}

void intro(void)
{
	cout << "This application consists of a few console games\n";
	cout << "traditionally found in casinos or other places\n";
	cout << "where people go to gamble.";
	cout << endl << endl;
}

void menu(void)
{
	char choice;
start:
	do
	{
		cout << "Which game would you like to play?\n";
		cout << "(1 = Craps, 2 = Slot machines, 3 = Poker, E = EXIT)\n";
		
		cin >> choice;
		
		cout << endl << endl;

		switch(choice)
		{
		case '1':
			craps();
			break;
		
		case '2':
			slots();
			break;
		
		case '3':
			poker();
			break;

		case 'e':
			break;
			break;
		}
	}while((choice != 'e' || choice != 'E') && cash >= 10);
	
	if (cash < 10)
	{
		cout << "Would you like to play again (Y/N)?  ";
		cin >> choice;
		
		switch(choice)
		{
		case 'Y':
			cash = 1000;
			cout << "You found 1000$ worth of poker chips in the garbage :)" << endl << endl;
			goto start;
			break;
		case 'y':
			cash = 1000;
			cout << "You found 1000$ worth of poker chips in the garbage :)" << endl << endl;
			goto start;

		case 'N':
			break;
		case 'n':
			break;
		default:
			break;
		}
	}
}


void craps(void)
{
	cout<<"Craps table\n\n";
	crapstable table1;
	
	do
	{
		table1.showcash();

		table1.getbet(bet);
		if (bet = 0){ break;}
	
		table1.getwager(wager);
		
		dices = table1.rolldice();
		
		cout << "You rolled a " << dices << endl << endl;
		
		table1.win(dices, wager, bet,winnings);
		
		if (winnings > 0)
		{
			cout << "You won: " << winnings << "$\n\n";
			cash += winnings;
		}
			else
		{
			cout << "You lost the bet.\n";
			cash -= wager;
		}

		cout << "dices = " << dices << "\tbet = " << bet << "\twinnings = " << winnings << endl;
		system("pause");
	}
	while(cash > 10);
	
	if(cash <= 10) cout << "You are broke.  You should watch your gambling problem" << endl;
	if(cash > 10) cout << "You leave the craps table with " << cash << "$" << endl;

}

void slots(void)
{
	cout<<"slots\n";
}

void poker(void)
{
	cout<<"poker\n";
  
and gamblingclasses.h ...
        
      
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

void intro(void);
void menu(void) ;
void craps(void);
void slots(void);
void poker(void);

int cash=1000,dices;

class crapstable
	{
		private:
			int Cwager, Cwinnings, Closs, Cbet, Cdicesum, dice;
		
		public:
			void showcash(void)
			{
				cout << "You currently have " << cash << "$" << endl << endl;
			}

			void getbet(int& Cbet)
			{
				cout << "Please enter bet (1 : 6/8, 2 : 4/10, 3 : 2/12, 0: EXIT):";
				cin  >> Cbet;
				cout << endl;
			}
					
			void getwager(int& Cwager)
			{
				cout << "Please place wager (minimum of 10$, maximum of 100$):";
				cin >> Cwager;
				if (Cwager < 10 && Cwager != 0) Cwager = 10;
				if (Cwager > 100) Cwager = 100;
				cout << endl << endl;
			}
				
			int rolldice(void)
			{
				srand (time (NULL));
				dice = (rand() % 11);
				dice += 2;
				return dice;	
			}
			
			void win(int dice, int Cwager, int Cbet, int& Cwinnings)
			{
				Cwinnings = 0;
				switch(Cbet)
				{
				case 1:
					if(dice == 6 || dice == 8)	Cwinnings = 1.5 * Cwager;
					else Cwinnings = -Cbet;
					break;
					
				case 2:
					if(dice == 4 || dice == 10)	Cwinnings = 2 * Cwager;
					else Cwinnings = Cbet * -1;
					break;

				case 3:
					if(dice == 2 || dice == 12)	Cwinnings = 5 * Cwager;
					else Cwinnings = -Cbet;
					break;
				
				}

			}

	};
      
[edited by - Infuscare on October 17, 2002 6:49:49 PM]

Share this post


Link to post
Share on other sites
You are using GOTO statements. Goto is a sign of bad programming and CAN ALWAYS be avoided. First replace the goto with a loop. I haven''t really checked the code because I always get lost in GOTO spaghetti. Fix it, and then have a new look, prolly the problem might solve itself.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site

Share this post


Link to post
Share on other sites
The "if (bet = 0)" statement in the craps function is resetting the value of the bet variable to always be 0. Change it to "if (bet == 0)" and I believe you will get some good results.

Your code could use some rework, but not bad over all. It looks like you have some confusion about seeding the rand function though. You should move the call to srand to your class constructor so that it''s only called once. By calling it every time you throw the die you are not giving the player a fair chance.

Also, the way you''ve coded things, your crapstable member variables are not being used. You should either change your member functions to use them instead of the parameters passed, or just get rid of them entirely.

Hope this helps.

Share this post


Link to post
Share on other sites
Thanks a lot guys...

Ps. i know goto is bad programming but I didn't feel like making another loop just for that...

[edited by - Infuscare on October 18, 2002 1:03:10 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i find it bad practice to use gotos all over the place, but a well placed goto can be very good optimization and lessen the complexity of certain pieces of code.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i find it bad practice to use gotos all over the place, but a well placed goto can be very good optimization and/or lessen the complexity of certain pieces of code.

Share this post


Link to post
Share on other sites
I understand that goto contributed to spaghetti code problems and therefore is a bad idea in the minds of most programers. However is there any reason to avoid it all together? If it is used intellegently why not make use of it?

Any thoughts as to why it is bad code instead of that ... it just is?

"... thats the rub...

[edited by - hamlet on October 18, 2002 5:07:42 PM]

Share this post


Link to post
Share on other sites
I have been using C++ for 3 years on and off.. a lot in the last 8 months. And I have never found the need for a goto statement. I program in BASIC at work, and hate using them.. and try not to even in that. Even before I knew goto's were bad programming practice I disliked them, because I realised the potential for having to read through your code.

If you write a loop, the person reading through can easily see where the loop begins and ends, and with good commenting you needn't worry about it. Writing gotos makes the code difficult to maintain and very hard to read back, even with lots of comments. In small code I suppose it would be ok, but when you are talking about things like game code, maybe your entire game logic, reading it back would just be a nightmare I should imagine.

PS - Its bad PRACTICE, not code. Teams of programmers reading one anothers code don't wanna keep having to jump about the source do they.

=*=
If things seem bad, think that they can get a whole load worse, and they don't seem so bad anymore

=*=

[edited by - hammerstein_02 on October 18, 2002 5:48:51 PM]

Share this post


Link to post
Share on other sites
ummm,

You told me a little higher up that I should put

SRAND(time(NULL))

in the constructor... how do i do that,
I know how to put variables in there but just
sticking that in the constructor doesn''t work

_________________________________________
"Why, why why does everyone ask ''''why'''' when ''''how'''' is so much more fun"
-Spawn 1997

Share this post


Link to post
Share on other sites
srand(time(NULL));

that seeds the random number generator (you only need to do it once) don''t forget C++ is case sensitive. It wouldn''t work if you were putting SRAND(time(NULL))

just put it inside your constructor code.

=*=
If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore

=*=

Share this post


Link to post
Share on other sites