Combining arrays

When I try to combine these arrays, the enemy array overwrites part of the player array inside of the enco array leaving me without a player. I can't seem to get it to work correctly... any help pointing out the blindingly obvious error that i have to be making would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
... // Cut
    void combineALL ()
    {
        delete[] enco;
        ALL = PLAYERS + ENEMIES;
        enco = new character[ALL];
        for (int i = 0; i < ALL; ) // Feeds all players[] and enemies[] into enco[]
        {
            for (int j = 0; j < PLAYERS; i++, j++)
            {
                enco[i] = player[j];
            };
            for (int j = 0; j < ENEMIES; i++, j++)
            {
                enco[i] = enemy[j];
            };
        };
    }
... // Cut 
That first for loop over i is useless as it should only run once if your following for loops are correct.

Also, you have a useless ; after your for loops, but that won't be a problem here.

Your code looks alright to me; have you tried stepping through it with a debugger?
Yes I have stepped through it with a debugger, but I don't understand what it tells me. watching the i and j variables it looks like it should be working, but its not...

Edit:
I took out the for loop and created this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    void combineALL ()
    {
        delete[] enco;
        ALL = PLAYERS + ENEMIES;
        enco = new character[ALL];
        int i =0;// Feeds all players[] and enemies[] into enco[]
        for (int j = 0; j < PLAYERS; i++, j++)
        {
            enco[i] = player[j];
        };
        for (int j = 0; j < ENEMIES; i++, j++)
        {
            enco[i] = enemy[j];
        };
    }


but now I get a seg fault error when I debug..
Last edited on
Topic archived. No new replies allowed.