Craps game will not exit

I wrote the code for a game of craps but when I hit x to exit it runs

Page 1 of 1

5 Replies - 940 Views - Last Post: 21 October 2009 - 04:52 AM Rate Topic: -----

#1 jthamrick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-November 08

Craps game will not exit

Posted 20 October 2009 - 06:11 PM

I wrote this code and the problem I am having is when I hit x after it prompts you for a bet it runs an infinite loop. If I get rid of the goto statements it will run once no problem no matter what number you roll.

Help with this would be greatly appreciated.

#include <iostream>

using namespace std;

#include <stdlib.h>	
#include <time.h>

int ThrowDice ();

void main ()
	{
	int Value = 0;
	int Total = 50;
	int Bet = 0;
	int Point = 0;
	int ValueTwo = 0;
	char Answer = 0;

	cout << "Welcome to the game of Craps" << endl;
	cout << "Starting balance - $50" << endl;
	cout << "To exit the game hit X and press return when you are asked to bet\n" << endl;

	top:
	
	if (Total == 0)
		{
		cout << "You're broke, better luck next time." << endl;
		exit (0);
		}
	else;

	cout << "Enter your bet: ";
	cin >> Bet;

	while (Bet > Total)
		{
		Bet = 0;
		cout << "Bet is too high - enter another bet: ";
		cin >> Bet;
		}

	if (Bet == 'X')
		exit (0);
	else;
	if (Bet == 'x')
		exit (0);
	else;
			
	srand (time (0));
	Value = ThrowDice ();
	cout << "The value of the roll is " << Value << endl;

	switch (Value)
		{
		case 2:
			Total -= Bet;
			cout << "Your total is now: $" << Total << endl;
			goto top;
			break;
		case 3:
			Total -= Bet;
			cout << "Your total is now: $" << Total << endl;
			goto top;
			break;
		case 4:
			Point = Value;
			cout << "Roll again to see if you can match the point: Y/N  ";
			cin >> Answer;
			if ((Answer == 'y') || (Answer == 'Y'))
				{
				do  {
					Value = ThrowDice ();
					cout << "The value of the roll is " << Value << endl;
					}while ((Value != Point) && (Value != 7));
				}
			else;
			if (Value == Point)
				{
				Total += Bet;
				cout << "You win, your total is $" << Total << endl;
				}
			else
				{
				Total -= Bet;
				cout << "You lose, your total is $" << Total << endl;
				}
			goto top;
			break;
		case 5:
			Point = Value;
			cout << "Roll again to see if you can match the point: Y/N  ";
			cin >> Answer;
			if ((Answer == 'y') || (Answer == 'Y'))
				{
				do  {
					Value = ThrowDice ();
					cout << "The value of the roll is " << Value << endl;
					}while ((Value != Point) && (Value != 7));
				}
			else;
			if (Value == Point)
				{
				Total += Bet;
				cout << "You win, your total is $" << Total << endl;
				}
			else
				{
				Total -= Bet;
				cout << "You lose, your total is $" << Total << endl;
				}
			goto top;
			break;
		case 6:
			Point = Value;
			cout << "Roll again to see if you can match the point: Y/N  ";
			cin >> Answer;
			if ((Answer == 'y') || (Answer == 'Y'))
				{
				do  {
					Value = ThrowDice ();
					cout << "The value of the roll is " << Value << endl;
					}while ((Value != Point) && (Value != 7));
				}
			else;
			if (Value == Point)
				{
				Total += Bet;
				cout << "You win, your total is $" << Total << endl;
				}
			else
				{
				Total -= Bet;
				cout << "You lose, your total is $" << Total << endl;
				}
			goto top;
			break;
		case 7:
			Total += Bet;
			cout << "Your total is now: $" << Total << endl;
			goto top;
			break;
		case 8:
			Point = Value;
			cout << "Roll again to see if you can match the point: Y/N  ";
			cin >> Answer;
			if ((Answer == 'y') || (Answer == 'Y'))
				{
				do  {
					Value = ThrowDice ();
					cout << "The value of the roll is " << Value << endl;
					}while ((Value != Point) && (Value != 7));
				}
			else;
			if (Value == Point)
				{
				Total += Bet;
				cout << "You win, your total is $" << Total << endl;
				}
			else
				{
				Total -= Bet;
				cout << "You lose, your total is $" << Total << endl;
				}
			goto top;
			break;
		case 9:
			Point = Value;
			cout << "Roll again to see if you can match the point: Y/N  ";
			cin >> Answer;
			if ((Answer == 'y') || (Answer == 'Y'))
				{
				do  {
					Value = ThrowDice ();
					cout << "The value of the roll is " << Value << endl;
					}while ((Value != Point) && (Value != 7));
				}
			else;
			if (Value == Point)
				{
				Total += Bet;
				cout << "You win, your total is $" << Total << endl;
				}
			else
				{
				Total -= Bet;
				cout << "You lose, your total is $" << Total << endl;
				}
			goto top;
			break;
		case 10:
			Point = Value;
			cout << "Roll again to see if you can match the point: Y/N  ";
			cin >> Answer;
			if ((Answer == 'y') || (Answer == 'Y'))
				{
				do  {
					Value = ThrowDice ();
					cout << "The value of the roll is " << Value << endl;
					}while ((Value != Point) && (Value != 7));
				}
			else;
			if (Value == Point)
				{
				Total += Bet;
				cout << "You win, your total is $" << Total << endl;
				}
			else
				{
				Total -= Bet;
				cout << "You lose, your total is $" << Total << endl;
				}
			goto top;
			break;
		case 11:
			Total += Bet;
			cout << "Your total is now: $" << Total << endl;
			goto top;
			break;
		case 12:
			Total -= Bet;
			cout << "Your total is now: $" << Total << endl;
			goto top;
			break;
		default:
			break;
		}
}

int ThrowDice ()
	{
	int Die1;
	int Die2;

	Die1 = (rand () % 6) + 1;
	Die2 = (rand () % 6) + 1;
	return Die1 + Die2;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Craps game will not exit

#2 zhanfeng  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 75
  • Joined: 09-November 08

Re: Craps game will not exit

Posted 20 October 2009 - 06:17 PM

You can get the amount and store it in a string and check if the string can be converted into a numeric value.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Craps game will not exit

Posted 20 October 2009 - 06:32 PM

There are so many things...

Short answer, "Bet" can only be an int; the user can't enter an 'X'. Ask the user to enter a 0 to exit.

Never use goto.

The logic stream is almost impossible to follow. On the first roll, if they hit 7 or 11 they win, 2 or 12 they loose. After that, look for point. Trying to do it all in the same check is prone to failure.

This post has been edited by baavgai: 20 October 2009 - 06:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 jthamrick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-November 08

Re: Craps game will not exit

Posted 20 October 2009 - 06:47 PM

I got it to work with an int for the exit, thanks baavgai.

Also I do have a few questions:

What is wrong with goto?

What are a few good practices to make sure I have a better logic stream and cleaner code?

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#5 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: Craps game will not exit

Posted 20 October 2009 - 09:02 PM

you can set up diffrent functions, and instead of using 'goto' it will make a function call
//instead of this
loop:
cout<<"Enter a name:\n";
cin>>name;
if (name<0)
goto loop;



//this is a little eaiser to fallow
do
{
name=MyName();
}while(name<0);


Was This Post Helpful? 1
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Craps game will not exit

Posted 21 October 2009 - 04:52 AM

View Postjthamrick, on 20 Oct, 2009 - 07:47 PM, said:

What is wrong with goto?


In a nutshell, it tends create cockroach style bugs that are real hard to catch and squish. We've argued about it on the board before. There's lots on the web. In 1968 Dijkstra published his famous Go To Statement Considered Harmful which pretty much coffin nailed the thing. I'm honestly surprised it's still taught.

View Postjthamrick, on 20 Oct, 2009 - 07:47 PM, said:

What are a few good practices to make sure I have a better logic stream and cleaner code?


Lots. ;) Use more functions; trying to put the logic all in main clutters stuff up. Use consistent syntax; in C++ uppercase is usually reserved for classes, not vars.

Here's a snippet of how I might write your project:
cout << "Current balance - $" << balance << endl;
cout << "Press return when you are asked to bet,  exit the game enter a 0 bet\n" << endl;

int bet = getBet(balance);
if (bet==0) { return; }

int point = throwDice();
cout << "The value of the roll is " << point << endl;
if (point==2 || point==12) {
	balance -= bet;
} else if (point==7 || point==11) {
	balance += bet;
} else {
	if (!rollAgain()) { return; }
	// while loop for the rest
	//...


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1