What is the difference?

I've written a program. It works. Problem is, the whole thing (mostly) was in main. Now, I'm going back and plugging in functions where I can. The problem is, one of my loops seems to be re-initializing, so the loop won't work. My question about this is:
Why is the code BEFORE the do-while loop executing (the outer loop) if I'm still in my inner loop?

I've set the two programs up and compared them side by side all day, and I still can't see why they are not behaving the same.

Can someone please help me find the problem?

The part of the program that isn't looping correctly is here:

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
int x = 0;//Initialize counter to simulate 500 times
    for (x = 0; x < 500; x++)//Loop iterates 500 times
    {
        initializeIsland(island, 10);//Set all cells in table to beginning values
        int row = 6;//Put Harvey back at beginning of bridge
        int col = 0;
        int costPerStep = 0;//Resets cost to 0 each iteration
        int harveysPos = 0;//Set Harvey to the value on the bridge

        do//Do all this...
        {
            takeStep();//Call function
            getDirection(randomNum, row, col);//Call function
            moveHarvey(direction, island, randomNum, row, col, harveysPos, costPerStep,
                       finalCost);//Call function

        //While Harvey hasn't fallen in the water or crossed to a bridge successfully
        }while (harveysPos == 10 || harveysPos == 5 || harveysPos == 0 || harveysPos == -1);
        //Fall out of inner loop 1 once Harvey falls into the water or crosses the bridge

        do//Do all this...
        {
            getOutput(harveysPos, totalRescues, madeItToBridge);//Call function

        //While Harvey has either fallen in the water or crossed to a bridge
        }while (harveysPos == 3 || harveysPos == 2);
        //Fall out of inner loop 2 once simulation has complete one time

    }//Fall out of outermost loop once simulation has been performed 500 times 

Once I go through the first inner loop and get back to line 10, it seems to have run the code in lines 5-8. Should that be happening?

Last edited on
Get a debugger and execute the program step by step.
You will notice that the line sequence is correct.

About your program not working, I guess that you never modify those variables. Will need to see those functions (especially the prototype)
I've been using the debugger all day. That's what I meant when I said that it seems to re-initialize. (The variables are modified in the other function, btw.) The debugger does not move up to line 6, but when it jumps from the bottom of the first inner loop back up to the beginning of the inner loop, all of a sudden, it's re-initialized again. I can't see where that's taking place, because the only place that should happen is in lines 5-8.

But you said it looks correct to you? Well, I though so too, but I'm obviously missing something...
(The variables are modified in the other function, btw.)
maybe you are modifying a copy.
Hmmm. That may just be it! Does that mean I am supposed to be passing by reference to avoid that? I'll try that real quick and see if it fixes anything.
That was it. Thanks so much, that was so helpful!
Topic archived. No new replies allowed.