gpt4 book ai didi

c - scanf 直到我按 Enter 两次才接受输入

转载 作者:行者123 更新时间:2023-11-30 16:10:34 24 4
gpt4 key购买 nike

不会跳过 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;
}

最佳答案

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

关于c - scanf 直到我按 Enter 两次才接受输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58864503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com