mercredi 22 novembre 2017

C - An if-else statement gets stuck and crashes

I have a problem with an code where i have and if-else statement and it needs an input of an integer and otherwise would ask again to enter the number. The code is in a while statement and the problem is that when i enter anything else than an integer the loop gets stuck giving the else statement and crashes

static inline void number_console(void)
{
int x = 0;
fprintf_P(stdout, PSTR(GET_NR_MSG));
lcd_goto(0x40);

if (scanf("%d", &x) == 1 && x >= 0 && x <= 9) {
    printf("\nYou entered number: ");
    fprintf_P(stdout, (PGM_P)pgm_read_word(&numbers[x]));
    fputc('\n', stdout);
    lcd_puts_P((PGM_P)pgm_read_word(&numbers[x]));
    lcd_putc(' ');
} else {
    printf("invalid input\n");
    }
}

Also the code is used in a while statement

while (1) {
    blink_leds();
    number_console();
}

Aucun commentaire:

Enregistrer un commentaire