【问题标题】:scanf doesn't take the input until I hit enter twicescanf 在我按两次 Enter 之前不接受输入
【发布时间】:2019-11-14 19:35:13
【问题描述】:

scanf 不会被跳过。 它等待我输入输入,但我必须输入值并按两次回车(第二个输入已正确保存)。

int yes(void)
{
    int yes, rc, isValid;
    char answer, nl = 'a';

    do {
        clearKeyboard();
        rc = scanf(" %c%c", &answer, &nl);
        if (nl == '\n')  { //|| rc != 2
            if ((answer == 'y') || answer == 'Y') {
                yes = 1;
                isValid = 1;
            }
            else if ((answer == 'n') || answer == 'N') {
                yes = 0;
                isValid = 1;
            }
            else {
                printf("*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: ");
                isValid = 0;
                clearKeyboard();
            }
        }
        else {
            printf("*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: ");
            isValid = 0;
            clearKeyboard();
        }
    } while (!isValid);

    return yes;
}

【问题讨论】:

标签: c scanf


【解决方案1】:

我建议不要使用scanf,但既然你是:nl 的目的是什么?如果您删除scanf 中的第二个%c,您将获得您的answer,并且只需要输入一次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 2023-01-08
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多