Simulation - Running Total

I am writing a simulation program and everything works fine except the running total.

The simulation loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  for (int sim = 0; sim < SIMS; sim++)
    {
        int cost = 0, action = 0;
        
        while (action >= 0 && action <= 2)
        {
            step = rand() % (100 + 1);
            //cout << step << endl;
            getMove (step, rPos, cPos);
            action = island[rPos][cPos];
            //cout << action << endl;
            cost += getResult (island, rPos, cPos, action, completed, rescued);
        }
        
        totalCost += cost;
        cout << totalCost;
    }


The getResult function:
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
int getResult (int ary[][14], int r, int c, int action, int &complete, int &rescue)
{
    int cost = 0;
    //Fell in water. Must be rescued.
    if (action == -1)
    {
        rescue++;
    }
    
    //Made it to one of the two bridges.
    if (action == 3)
    {
        complete++;
    }
    
    //Stepped on one of two flowers in a cell. Owes $5.
    if (action == 2)
    {
        cost = 5;
        ary[r][c]--;
    }
    
    //Stepped on the last flower in a cell. Owes $5.
    if (action == 1)
    {
        cost = 5;
        ary[r][c]--;
    }
    
    return cost;
}


When I print out totalCost I get something like:

9595959510010010010010012512512
5125125125125125125125125125125
1251251251251251251251251251251
2512512512512512512512512512512
5125125125125125125125125125125
1251251251251251251351351351351
3513513513513513513513513513513
5135135135145145145145145145145
1451451451451451451451451451451
5015015015015015015015015015015
0150150150150150150150150150150
1501501501501501501501501501501
5015015015015015015015015015015


I'm new to simulation and haven't found any info on what would cause this.
OK, I just realized my cout << totalCost; was in a very bad place.
Topic archived. No new replies allowed.