Reputation: 1163
In this part of code is an error. It says
"Run-Time Check Failure #3 - The variable 'direct' is being used without being initialized."
How can I fix this error ?
bool loop=true;
int direct;
al_install_keyboard();
ALLEGRO_TIMER *timer = al_create_timer (0.4);
ALLEGRO_EVENT_QUEUE *keystroke = al_create_event_queue();
al_register_event_source(keystroke ,al_get_keyboard_event_source() );
al_register_event_source (keystroke , al_get_timer_event_source(timer));
al_start_timer(timer);
do{
ALLEGRO_EVENT input;
al_wait_for_event(keystroke , &input );
if (input.type == ALLEGRO_EVENT_KEY_DOWN){
switch(input.keyboard.keycode){
case ALLEGRO_KEY_DOWN: direct=DOWN; break;
case ALLEGRO_KEY_UP : direct=UP;break;
case ALLEGRO_KEY_LEFT: direct=LEFT;break;
case ALLEGRO_KEY_RIGHT: direct=RIGHT;break;
case ALLEGRO_KEY_ESCAPE: loop= false;
}
}
if(input.type==ALLEGRO_EVENT_TIMER){
switch(direct){
case DOWN: moove_snake(DOWN) ; break;
case UP: moove_snake(UP) ; break;
case LEFT: moove_snake(LEFT) ; break;
case RIGHT: moove_snake(RIGHT) ; break;
}
}
}while (loop);
Upvotes: 1
Views: 28983
Reputation: 1342
If the first if condition if (input.type == ALLEGRO_EVENT_KEY_DOWN)
is false and the second one if(input.type==ALLEGRO_EVENT_TIMER)
is true, you are using switch
with an uninitialized variable (int direct). This means that the variable is pointing to random bits in the memory and your compiler doesn't like that fact.
It's easy to fix by initializing the variable in the beginning. For example:
int direct = UP;
Upvotes: 3
Reputation: 512
READ CAREFULLY!! When the message appears, you have to choose break instead of continue before closing your application. If it still doesn't work, put static method before the variable like
static int direct;
Upvotes: 0
Reputation: 2948
If you have tried to initialize it and it did not work, then likely you have a problem elsewhere in the code that the parser just happens to fail on this line. Look above the int direct;
line.
Check that all your curly braces match, that you have semi colons at the ends of lines, and that everything else generally looks good.
If you don't find it, comment out everything starting at int direct;
and go from there.
Upvotes: 1