for loop warnings, help request


Hi guys,


I've got my code in progress. It compiles fine, runs fine, no problems whatsoever; however, when I compile it, it returns a warning:

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <SDL.h>
using namespace std;


class Cube{
public:
    Cube(int x,int y,int w,int h) { XPos = x; YPos = y; width = w; height = h; box.x = XPos; box.y = YPos; box.w = width; box.h = height;}
    SDL_Rect box;

private:
    int XPos;
    int YPos;
    int width;
    int height;
};

class Enemy{
public:
    Enemy(int x,int y) { xPos = x; yPos = y; width = 50; height = 15; box.x = xPos; box.y = yPos; box.w = width; box.h = height;}
    SDL_Rect box;

private:
    int xPos;
    int yPos;
    int width;
    int height;
};

//------------------------------COLLISION DETECTION-----------------------------------//

//bool collision(SDL_Rect* rec1, SDL_Rect* rec2)
//{
//    if(rec1->y >= rec2->y + rec2->h)
//    {
//        return false;
//    }
//    if(rec1->y + rec1->h <= rec2->y)
//    {
//        return false;
//    }
//    if(rec1->x >= rec2->x + rec2->w)
//    {
//        return false;
//    }
//    if(rec1->x + rec1->w <= rec2->x)
//    {
//        return false;
//    }
//    return true;
//}

//------------------------------------------------------------------------------------//

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface *screen, *ship, *enemy, *bullet;
    screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    ship = SDL_DisplayFormat(SDL_LoadBMP("ship1.bmp"));
    SDL_SetColorKey(ship, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
    enemy = SDL_DisplayFormat(SDL_LoadBMP("enemy.bmp"));
    SDL_SetColorKey(enemy, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
    bullet = SDL_DisplayFormat(SDL_LoadBMP("bullet.bmp"));
    SDL_SetColorKey(bullet, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
    bool running = true;
    bool b[2] = {0,0};
    Uint32 start;
    int unsigned second = 1000;
    int unsigned fps = 60;
    SDL_Event event;
    Uint32 black = SDL_MapRGB(screen->format,0,0,0);
    Cube * Box = new Cube(125,450,65,15);
    int unsigned paddleVel = 3;
    int unsigned i = 0;
    int unsigned k = 6;
    int unsigned enemyX = 80;
    int unsigned enemyY = 30;
    int unsigned const opponents = 24;
    Enemy * opponent[opponents];
    for(int unsigned row = 0; row < (opponents / 6); row++ )
    {
        for( i; i < k; i++ )
        {
           opponent[i] = new Enemy(enemyX,enemyY);
           enemyX = enemyX + 85;
        }
        enemyX = 80;
        enemyY += 30;
        k += 6;
    }
    while(running)
    {
            start = SDL_GetTicks();
            while(SDL_PollEvent(&event))
            {
                switch(event.type)
                {
                case SDL_QUIT:
                    running = false;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                    case SDLK_ESCAPE:
                        running = false;
                        break;
                    case SDLK_LEFT:
                        b[0] = 1;
                        break;
                    case SDLK_RIGHT:
                        b[1] = 1;
                        break;
                    }
                break;
                case SDL_KEYUP:
                    switch(event.key.keysym.sym)
                    {
                    case SDLK_LEFT:
                        b[0] = 0;
                        break;
                    case SDLK_RIGHT:
                        b[1] = 0;
                        break;
                    }
                    break;
                }
            }
//--------------------------ENEMY MOVEMENT-----------------------------//

//--------------------------BULLET MOVEMENT----------------------------//

//--------------------------SHIP MOVEMENT------------------------------//
            if(b[0]){
                Box->box.x -= paddleVel;
                if(Box->box.x <= 0){
                        Box->box.x += paddleVel;
                }
            }
            if(b[1]){
                Box->box.x += paddleVel;
                if(Box->box.x + Box->box.w >= 640){
                        Box->box.x -= paddleVel;
                }
            }
//----------------------------RENDER-----------------------------------//
            SDL_FillRect(screen,&screen->clip_rect,black);
            SDL_BlitSurface(ship, NULL, screen, &Box->box);
            for(int unsigned j = 0; j < opponents; j++)
            {
                SDL_BlitSurface(enemy, NULL, screen, &opponent[j]->box);
            }
            SDL_Flip(screen);
            if(second/fps>(SDL_GetTicks()-start))
                SDL_Delay(second/fps-(SDL_GetTicks()-start));
    }
    SDL_FreeSurface(screen);
    SDL_FreeSurface(ship);
    SDL_FreeSurface(enemy);
    SDL_FreeSurface(bullet);
    SDL_Quit();
    return 0;
}



main.cpp|82|warning: statement has no effect [-Wunused-value]|

As you can see, line 82 returns with this warning. Like I said, nothing works other than intended, everything compiles, everything runs, no problems. Any ideas as to what the issue is, and what I can do to correct it? I assume it is probably something with the i int for the first argument, but I just don't know what to do with it. Any help is appreciated. Thanks!
If you were to write something like
1
2
   int n = 0;
   n;


The second line n; is perfectly legal C++. It evaluates n which is zero and then throws the result away.

On line 82: for( i; i < k; i++ )

The variable i was initialized to zero earlier outside of the loop on line 74. Consequently, the i; part of
for( i; i < k; i++ ) is evaluated to zero and the result thrown away. Replace the i; with a null statement.
Try changing line 82 to:

for( ; i < k; i++ )

and see if the warning goes away.
That actually does it! Thanks! We learn something new every day. :)

Hi again.

I've actually moved on, and have come across another issue. I've changed my code a bit, moving things out for global variables, adding an external function, etc:

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <SDL.h>
using namespace std;


class Cube{
public:
    Cube(int x,int y,int w,int h) { XPos = x; YPos = y; width = w; height = h; box.x = XPos; box.y = YPos; box.w = width; box.h = height;}
    SDL_Rect box;

private:
    int XPos;
    int YPos;
    int width;
    int height;
};

class Enemy{
public:
    Enemy(int x,int y) { xPos = x; yPos = y; width = 50; height = 15; box.x = xPos; box.y = yPos; box.w = width; box.h = height;}
    SDL_Rect box;

private:
    int xPos;
    int yPos;
    int width;
    int height;
};

//------------------------------COLLISION DETECTION-----------------------------------//

//bool collision(SDL_Rect* rec1, SDL_Rect* rec2)
//{
//    if(rec1->y >= rec2->y + rec2->h)
//    {
//        return false;
//    }
//    if(rec1->y + rec1->h <= rec2->y)
//    {
//        return false;
//    }
//    if(rec1->x >= rec2->x + rec2->w)
//    {
//        return false;
//    }
//    if(rec1->x + rec1->w <= rec2->x)
//    {
//        return false;
//    }
//    return true;
//}

//------------------------------------------------------------------------------------//

void arrayMove();
int unsigned const opponents = 24;
int unsigned tmpEnemyVel;
Enemy * opponent[opponents];
int unsigned const lastOpponent = (opponents - 1);

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface *screen, *ship, *enemy, *bullet;
    screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    ship = SDL_DisplayFormat(SDL_LoadBMP("ship1.bmp"));
    SDL_SetColorKey(ship, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
    enemy = SDL_DisplayFormat(SDL_LoadBMP("enemy.bmp"));
    SDL_SetColorKey(enemy, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
    bullet = SDL_DisplayFormat(SDL_LoadBMP("bullet.bmp"));
    SDL_SetColorKey(bullet, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
    bool running = true;
    bool b[2] = {0,0};
    Uint32 start;
    int unsigned second = 1000;
    int unsigned fps = 60;
    SDL_Event event;
    Uint32 black = SDL_MapRGB(screen->format,0,0,0);
    Cube * Box = new Cube(125,450,65,15);
    int tmpTimer = fps;
    int enemyVel = 10;
    tmpEnemyVel = enemyVel;
    int unsigned paddleVel = 3;
    int unsigned i = 0;
    int unsigned k = 6;
    int unsigned enemyX = 80;
    int unsigned enemyY = 30;
    for(int unsigned row = 0; row < (opponents / 6); row++ )
    {
        for(; i < k; i++ )
        {
           opponent[i] = new Enemy(enemyX,enemyY);
           enemyX = enemyX + 85;
        }
        enemyX = 80;
        enemyY += 30;
        k += 6;
    }
    while(running)
    {
            start = SDL_GetTicks();
            while(SDL_PollEvent(&event))
            {
                switch(event.type)
                {
                case SDL_QUIT:
                    running = false;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                    case SDLK_ESCAPE:
                        running = false;
                        break;
                    case SDLK_LEFT:
                        b[0] = 1;
                        break;
                    case SDLK_RIGHT:
                        b[1] = 1;
                        break;
                    }
                break;
                case SDL_KEYUP:
                    switch(event.key.keysym.sym)
                    {
                    case SDLK_LEFT:
                        b[0] = 0;
                        break;
                    case SDLK_RIGHT:
                        b[1] = 0;
                        break;
                    }
                    break;
                }
            }
//--------------------------ENEMY MOVEMENT-----------------------------//
            tmpTimer -= 3;
            if(tmpTimer == 0)
            {
                arrayMove();
                tmpTimer = fps;
            }

//--------------------------BULLET MOVEMENT----------------------------//

//--------------------------SHIP MOVEMENT------------------------------//
            if(b[0]){
                Box->box.x -= paddleVel;
            if(Box->box.x <= 0){
                Box->box.x += paddleVel;
                }
            }
            if(b[1]){
                Box->box.x += paddleVel;
            if(Box->box.x + Box->box.w >= 640){
                Box->box.x -= paddleVel;
                }
            }
//----------------------------RENDER-----------------------------------//
            SDL_FillRect(screen,&screen->clip_rect,black);
            SDL_BlitSurface(ship, NULL, screen, &Box->box);
            for(int unsigned j = 0; j < opponents; j++)
            {
                SDL_BlitSurface(enemy, NULL, screen, &opponent[j]->box);
            }
            SDL_Flip(screen);
            if(second/fps>(SDL_GetTicks()-start))
                SDL_Delay(second/fps-(SDL_GetTicks()-start));
    }
    SDL_FreeSurface(screen);
    SDL_FreeSurface(ship);
    SDL_FreeSurface(enemy);
    SDL_FreeSurface(bullet);
    SDL_Quit();
    return 0;
}

void arrayMove()
{
    for(int unsigned m = 0; m < opponents; m++)
    {
        opponent[m]->box.x -= tmpEnemyVel;
        if(opponent[m]->box.x <= 30)
                    tmpEnemyVel = (10 * -1);
        if(opponent[lastOpponent]->box.x + opponent[lastOpponent]->box.w >= 610)
                    tmpEnemyVel = 10;
    }
}


So basically, my array of opponents moves just fine as set by the tmpTimer.
It's the collision that I'm having an issue with. When the entire array gets to the x = 30 mark, the enemyVel reverses, so that the array starts moving the opposite direction. The problem is that opponent[0] moves one more step in the x-= direction before changing to the x+= direction. Only opponent[0] does this. Once the array reaches the other side of the screen, everything works fine. On it's way back left, the same thing happens. When the array reaches x = 30, the entire array except opponent[0] turns around and proceeds in the opposite direction. The very next tick, opponent[0] turns around, but of course is now 20 pixels to the left of the rest of the array...

After looking at this for about a half hour, I must be overlooking something. I need to walk away for a bite to eat, lol. Anyone willing to take a look for me? Help is so greatly appreciated. I know I seem to hit a lot of brick walls, and I'm glad this forum is so upfront to help. Thanks!
Sorry guys. After getting a bite to eat, it gave me time to sit at the dinner table and think things over. I've figured out the problem. It seems in likes 181-185, the two if statements need to be made before the move command. I guess a sequential check, and it was incorrect. Thanks anyway for anyone who has looked this over! I'm sure I'll be back.
Topic archived. No new replies allowed.