Программа на C: Почему моя игра в угадайку сходит с ума, если вы вводите строку в переменную int [duplicate]

Правильно, это не сработает. Решение:

>>> 'The last:{0}'.format(a[-1])
'The last:3'
0
задан programmingnoob 1 March 2019 в 17:08
поделиться

1 ответ

При сбое scanf он не потребляет входные данные, поэтому вы будете заблокированы в бесконечном цикле.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int passcode;
    int guessamount = 0;

    printf("Try to guess the magic passcode:");
    scanf("%d", &passcode);

    guessamount++;

    while (passcode != 43) {
        if (passcode >= 101) {
            printf("The passcode is too high!It cannot exceed 100\n");
            guessamount--;
        }

        printf("The passcode was wrong.Please try again:");
        if (!scanf("%d", &passcode)) {
            printf("Invalid value\n");
            while (getchar() != '\n');
        }

        guessamount++;
    }
    printf("The passcode is true!You used %d tries!", guessamount);



    return 0;
}
0
ответ дан Vitor SRG 1 March 2019 в 17:08
поделиться
Другие вопросы по тегам:

Похожие вопросы: