'Counter" in craps program

Hello. I have written a code to run a game of craps...i cant figure out how to run it 100,000 times and count how many wins and losses there are and print the results. i have the craps code posted below. Please help me out i am stuck...

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
 #include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std;

int rolldice();
int main()
{
    enum Status { CONTINUE, WON, LOST };

    int counter = 1;
    int mypoint;
   {

   }

    Status gameStatus;

    srand( time(0));

    int sumofdice = rolldice();
    switch (sumofdice)

    {
    case 7:
    case 11:
        gameStatus = WON;
        break;
    case 2:
    case 3:
    case 12:
        gameStatus = LOST;
        break;
    default:
        gameStatus = CONTINUE;

            mypoint = sumofdice;
            
            cout << "Point is " << mypoint << endl;
            break;

    }

    while ( gameStatus == CONTINUE)
    {
        sumofdice = rolldice();

        if (sumofdice == mypoint )
            gameStatus = WON;
        else
            if ( sumofdice == 7 )
            gameStatus = LOST;
    }

    if (gameStatus == WON )
        cout << "Player Wins" << endl;
    else
        cout << "Player Loses" << endl;

}

int rolldice()
    {
        int die1 = 1 + rand() %6;
        int die2 = 1 + rand() %6;

        int sum = die1 + die2;

        cout << "Player Rolled " <<die1 << "+" << die2 << "=" << sum << endl;

        cin.get();

        return sum;

        cin.get();

    }
Last edited on
I would put the code int x = 1; after the srand(time(0));. Then before the int sumofdice = rolldice(); I would put the while loop where x is less than or equal to 100,000 and have the loop end after your other while loop. When a player was to win or lose, increment x. If you want to keep track of wins and lost, use variables for that also and increment them based on wins/losses.
I would put most of that code into its own function:
Status PlayCraps();

PlayCraps() can return WON, LOST, or CONTINUE. Then I'd put it all in a for loop:

1
2
3
int wins = 0;
for (int i = 0; i < 100000; ++i)
    wins += (PlayCraps() == WON) ? 1 : 0;
Topic archived. No new replies allowed.