Craps Program Help

Hi, so i have this program due for a CS class soon and im having trouble figuring out how to do it. At this point through in class help and the help of my peers i have a working craps program that runs 1000 times and increments a wins and losses value, but i can't figure out how to make it calculate and output the percentage of winning and losing after it runs. Here's the code i have, any advice is appreciated.



#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;
int wins;
int losses;




int rolldice ();

void main ()
{
enum Status {CONTINUE, WON, LOST};
int Sum, MyPoint;
int a = 0;
wins == 0;
losses ==0;
Status GameStatus;

srand(time(0));

//Roll the dice
for(a = 0; a <= 1000; a++)
{
Sum = rolldice();
cout << "You rolled " << Sum << "." << endl;

switch (Sum)
{
case 7:
case 11:
GameStatus = WON;
break;
case 2:
case 3:
case 12:
GameStatus = LOST;
break;
default:
GameStatus = CONTINUE;
MyPoint = Sum;
cout << "Point is " << MyPoint << '.' << endl;
break;
}

while (GameStatus == CONTINUE)
{
Sum = rolldice();
cout << "The roll is " << Sum << '.' << endl;
if (Sum == MyPoint)
GameStatus = WON;
else
if (Sum == 7)
GameStatus = LOST;

}

if (GameStatus == WON)
{

cout << "You Win the game." << endl;
wins++;
}

else
{
cout << "You Lose the game." << endl;
losses++;
}
}
}

int rolldice()
{
int Die1, Die2;
int Total;
Die1 = (rand() % 6) +1;
Die2 = (rand() % 6) +1;
Total = Die1 + Die2;
return Total;
}
That's basic math right there. You just declare 2 variables. 1 For lossPercentage and the other for winPercentage; then you just do winPercentage = (wins / 1000) and lossPercentage = (losses / 1000). Then print out the values.

No offense buts that very very basic C++
Because you actually did your homework and you only need help with basic math, I decided to go ahead and do it for you since I was bored anyway. I made it from scratch and redesigned it because yours was a little bit sloppy. Don't take it personally though, I am very OCD about my code and you're a beginner anyway.

Make sure you completely understand everything in the code if you're planning on turning it in as a homework assignment. I added a couple things that weren't there before including static_cast and references. Let me know if you have any questions but I don't check this site too often so I may not reply.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <ctime>

#define		GAMESTOPLAY		1000

enum eWinStatus
{
	LOSE,
	WIN,
	CONTINUE
};

int RandomizeDice()
{
	int iFirst = rand() % 6 + 1;
	int iSecond = rand() % 6 + 1;

	return ( iFirst + iSecond );
}

void PlayGame( int &iWins, int &iLosses )
{
	eWinStatus CurrentGame;
	int iDice = RandomizeDice();
	
	switch( iDice )
	{
	case 7:
	case 11:
		CurrentGame = WIN;
		break;
	case 2:
	case 3:
	case 12:
		CurrentGame = LOSE;
		break;
	default:
		CurrentGame = CONTINUE;
		break;
	}

	while ( CurrentGame == CONTINUE )
	{
		int iDealer = RandomizeDice();

		if ( iDealer == iDice )
			CurrentGame = WIN;

		if ( iDealer == 7 )
			CurrentGame = LOSE;
	}

	switch( CurrentGame )
	{
	case LOSE:
		std::cout << "You have lost the game." << std::endl;
		iLosses++;
		break;
	case WIN:
		std::cout << "You have won the game." << std::endl;
		iWins++;
		break;
	default:
		std::cout << "Error. Not counting this game." << std::endl;
		break;
	}
}

void main()
{
	srand( time( NULL ) );

	int iWins = 0, iLosses = 0;

	for ( int i = 0; i < GAMESTOPLAY; i ++ )
		PlayGame( iWins, iLosses );

	float flWinPercentage = static_cast<float>(iWins) / GAMESTOPLAY * 100;
	float flLossPercentage = static_cast<float>(iLosses) / GAMESTOPLAY * 100;

	std::cout << std::endl << std::endl;
	std::cout << "You have finished playing. Here are the final results..." << std::endl;
	std::cout << "Wins: " << iWins << std::endl;
	std::cout << "Win Percentage: " << flWinPercentage << std::endl;
	std::cout << "Losses: " << iLosses << std::endl;
	std::cout << "Loss Percentage: " << flLossPercentage << std::endl;
	std::cout << std::endl << std::endl;
}
Last edited on
Topic archived. No new replies allowed.