Function issue

Could someone explain why my function winCount isn't incrementing the win counters?

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  #include <iostream>
#include <ctime>
#include <cstdlib>

const float AARONACCURACY = 1.0/3;
const float BOBACCURACY = 0.5;
const float CHARLIEACCURACY = 1.0;
const int NUMDUELS = 1000;
int startDuel();
void shoot(bool, double);
void winCount(int, int&, int&, int&);
void showWins(int, int, int, int);



int main()
{
    srand(time(0));
    int charlieWins = 0;
    int bobWins = 0;
    int aaronWins = 0;
    for(int i = 0; i < 1000; i++ )
    {
        int victory = startDuel();

        winCount(victory, charlieWins, bobWins, aaronWins);
    }
    showWins(charlieWins, bobWins, aaronWins, NUMDUELS);



    return 0;
}

void shoot(bool &targetAlive, float accuracy)
{

    double randomAccuracy = 0;
    if(targetAlive)
    {
        randomAccuracy = rand() % 100;
        if(randomAccuracy < (accuracy * 100))
        {
            targetAlive = false;
        }

    }
    return;
}

int startDuel()
{
    bool aaronAlive = true;
    bool bobAlive = true;
    bool charlieAlive = true;
    do
    {


        //Aaron's turn
        if(aaronAlive)
        {
            if(charlieAlive == true)
            {
                shoot(charlieAlive, AARONACCURACY);
            }

            else if(bobAlive == true)
            {
                shoot(bobAlive, AARONACCURACY);
            }
        }

        //Bob's turn
        if(bobAlive)
        {
            if(charlieAlive == true)
            {
                shoot(charlieAlive, BOBACCURACY);
            }

            else if(aaronAlive == true)
            {
                shoot(aaronAlive, BOBACCURACY);
            }
        }

        //Charlie's turn
        if(charlieAlive)
        {
            if(bobAlive == true)
            {
                shoot(bobAlive, CHARLIEACCURACY);
            }
            else if(aaronAlive == true)
            {
                shoot(aaronAlive, CHARLIEACCURACY);
            }
        }
    }while((bobAlive && charlieAlive) || (bobAlive && aaronAlive) || (charlieAlive && aaronAlive));

    if(aaronAlive == true)
    {
        return 1;
    }
    else if(bobAlive)
    {
        return 2;
    }
    else if(charlieAlive)
    {
        return 3;
    }
}

void winCount(int whoWon,int &charliesWin,int &bobsWin,int &aaronsWin)
{

    switch(whoWon)
    {
    case 1: aaronsWin++;
    break;
    case 2: bobsWin++;
    break;
    case 3: charliesWin++;
    break;


    }
    return;
}

void showWins(int charliesWinCount, int bobsWinCount, int aaronsWinCount, int totalWins)
{
    std::cout << "Using the strategy of shooting the best player alive\n Aaron win %: " << aaronsWinCount / totalWins << std::endl;
    std::cout << "Bob win %: " << bobsWinCount / totalWins << std::endl;
    std::cout << "Charlie win %: " << charliesWinCount / totalWins;
    return;

}
It does. That is not the problem. The problem is the inter division that goes on in showWins.

aaronWinCount is an integer. And so is totalWins. Dividing two integers result in an integer. Meaning, if aaronsWinCount / totalWins becomes 0.3xx, which it does btw. It will round down to 0.

You fix this by simple changing one of them to float. You can either change All the WinCount variables to float, or just simply change totalwins to be of type float. If you divide an integer with a float, you will get a float.

const float NUMDUELS = 1000.0; // 1000.0 to make it a decimal float

void showWins(int, int, int, float); // changed last argument to float

void showWins(int charliesWinCount, int bobsWinCount, int aaronsWinCount, int totalWins) // changed last argument, totalWins, to float
Thanks. I went to my teacher because I was having problems and he wasn't able to figure it out.
Topic archived. No new replies allowed.