Probability Of Winning Is Zero?!

My program is supposed to simulate a specific number of games of craps "Pass Line" bets (I'm not kidding, it's called craps) and then display the number of wins, losses, and probability of winning, calculated as number of games won divided by games played. However, my probability of winning is always returned as zero.

The output is:

Welcome to Craps Pass Line Simulator. This application inputs a number of games and tells 
you the number of the times you won, the number of times you loss, and your probability of winning. 
How many games would you like to simulate? 
10000
You won: 8841 times. 
You loss: 1159 times. 
Your probability of winning is about 0%. 
Would you like to simulate more games? (y/n)
n
Thanks for using Craps Pass Line Simulator! 
Program ended with exit code: 0


Here is the code:
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
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cstdlib>
using namespace std;

int simulateGames (int games);

int main ()
{
    int games;
    char choice;
    double probability;
    
    cout << "Welcome to Craps Pass Line Simulator. This application inputs a number of games and tells "
         << "\nyou the number of the times you won, the number of times you loss, and your probability of winning. "
         << endl;
    do
    {
        cout << "How many games would you like to simulate? " << endl;
        cin >> games;
        
        probability = simulateGames (games);
        
        cout << "Your probability of winning is about " << probability << "%. " << endl;
        
        cout << "Would you like to simulate more games? (y/n)" << endl;
        cin >> choice;
        
    } while (choice == 'y' || choice == 'Y');
    
    cout << "Thanks for using Craps Pass Line Simulator! " << endl;
}

//Simulates a specific number of games and returns the probability of winning
int simulateGames (int games)
{
    int wins = 0, losses = 0, roll[2], point, total = 0;
    double probability = 0;
    bool won = false;
    
    //Loop through the specific number of games
    for (int i = 0; i < games; i++)
    {
        //Roll the two die
        roll[0] = (rand() % 6) + 1;
        roll[1] = (rand() % 6) + 1;
        
        //If the two rolls are either 7 or 11, you win
        if (roll[0] + roll[1] == 7 || roll[0] + roll[1] == 11)
            wins++;
        
        //If the two rolls are 2, 3, or 12, you lose
        else if (roll[0] + roll[1] == 2 || roll[0] + roll[1] == 3 || roll[0] + roll[1] == 12)
            losses++;
        
        //If it is not 2, 3, 4, 7, 11, 12, do this:
        else
        {
            //The "point" is the next two rolls. You must roll a point to win.
            point = roll[0] + roll[1];
            
            //Loop through until the total equals the point or 7
            while (total != point)
            {
                //The total of the two die
                total = roll[0] + roll[1];
                
                //If the total is the point, you win
                if (total == point)
                {
                    won = true;
                    continue;
                }
                
                //If the total is 7, you lose
                else if (total == 7)
                {
                    won = false;
                    break;
                }
            }
            
            //If you won, increase the number of wins
            if (won)
                wins++;
            
            //If not, increase the number of losses
            else if (!won)
                losses++;
        }
    }
    
    cout << "You won: " << wins << " times. " << endl;
    cout << "You loss: " << losses << " times. " << endl;
    
    //Calculate probability; THIS SEEMS NOT TO BE WORKING
    probability = wins/games;
    
    return probability;
}


I would appreciate any help.

Thanks,
VX
In c++ division on whole numbers returns a whole number, e.g 5/2 = 2. I don't have much time today,
but use double wins; instead (float or long double is an alternative).

Also, if you want your code to return probability in per cents, multiply the wins by 100 before division.
If you multiply by 100.0 you'll be able to still store wins as int.
Last option would be to "cast" wins to double before division.
Only one of the variables has to be a double to give you the type of division you want.

Edit: past tense of "lose" is "lost".
Last edited on
Topic archived. No new replies allowed.