N++ issue

I'm working on a shooting program and it's required to run 1,000 times. As far I know, my code is correct but it doesn't actually run 1,000 times. It's more like 650-700 times. Can anyone help me spot my error? I probably should include comments but i'll get to that later.
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
include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void shoot(bool& targetAlive, double accuracy);
//Simulates the shooting of a person
//take into account the accuracy of the person shooting

void startDuel();
//Shoots in order based on accuracy
//runs shoot 
//decides winner

double charlie_accuracy=1, bob_accuracy=.5, aaron_accuracy=.333;
bool charlieAlive=true, bobAlive=true, aaronAlive=true;
int charliewins=0, aaronwins=0, bobwins=0;


int main()
{
        int n=1000;
        srand(time(0));
        do
        {
                startDuel();
                n--;
        }while(n > 0);
        cout<<n<<endl;
        cout<<"Charlie won: "<< charliewins<<" number of times"<<endl;
        cout<<"Aaron won: "<<aaronwins<<" number of times"<<endl;
        cout<<"Bob won: "<<bobwins<<" number of times"<<endl;
        return 0;
}


void shoot(bool& targetAlive, double accuracy)
{
        double result;
        result = ((double) rand()/(RAND_MAX));
        cout<< result<<endl;
        if(result < accuracy)
        {
                targetAlive = false;
                cout<<"Target is dead\n";
        }
        else if(result > accuracy)
        {
                targetAlive = true;
                cout<<"Target is alive\n";
        }
}
void startDuel()
{
        shoot(charlieAlive, aaron_accuracy);
        if(charlieAlive==true)
        {
                shoot(charlieAlive, bob_accuracy);
                if(charlieAlive==true)
                {
                        shoot(bobAlive, charlie_accuracy);
                        if(bobAlive==false)
                        {
                                shoot(charlieAlive, aaron_accuracy);

                                if(charlieAlive==true)
                                {
                                        shoot(aaronAlive, charlie_accuracy);
                                        if(aaronAlive==false)
                                        {
                                                cout<<"Charlie Wins\n";
                                                charliewins++;
                                        }
                                }
                                else if(charlieAlive==false)
                                {
                                cout<<"Aaron Wins\n";
                                aaronwins++;
                                }
                        }
                }
                else if(charlieAlive==false)
                {
                        do
                        {
                                shoot(bobAlive, aaron_accuracy);
                                shoot(aaronAlive, bob_accuracy);
                        }while((bobAlive==true) && (aaronAlive==true));
                        if(bobAlive==true && aaronAlive==false)
                                {
                                        cout<<"Bob wins\n";
                                        bobwins++;
                                }
                                else if(aaronAlive==true && bobAlive==false)
                                {
                                        cout<<"Aaron wins\n";
                                        aaronwins++;
                                }
                }
        }
        else if(charlieAlive==false)
        {
                do
                {       
                        shoot(aaronAlive, bob_accuracy);
                        shoot(bobAlive, aaron_accuracy);
                }while((aaronAlive==true) && (bobAlive==true));
                if(bobAlive==false && aaronAlive==true)
                {
                        cout<<"Aaron wins\n";
                        aaronwins++;
                }
                else if(aaronAlive==false && bobAlive==true)
                {
                        cout<<"Bob Wins\n";
                        bobwins++;
                }

        }

}

                                                                                                                                                                                           
There are instances in your duels where nobody wins (everybody dies.) This can happen because you don't check to see if a person is dead before he shoots.

1
2
shoot(aaronAlive, bob_accuracy);
shoot(bobAlive, aaron_accuracy);


If aaron is shot in the first line there, he still gets to shoot at Bob in the second.
Lines 84-88 & 103-107: What happens if both aaron and bob will be dead after loop? It could easily happens, and nothing will be incremented.
oh ok. that makes sense. I thought that if bob shoots aaron, the loop would automatically exit because aaron being alive would then be set to false. I'll change it and then post what I did.
Thank you! I changed my code around a little and it's functioning correctly now. I appreciate the help.
Topic archived. No new replies allowed.