如何停止c中字符的重复无效答案?

如何停止c中字符的重复无效答案?,c,input,C,Input,我正在构建一个长程序,需要在if/else语句中使用典型的“y/n”char函数,它可以正常工作,但是如果用户输入了无效的内容,它将重复我输入的“无效答案”字符串,该字符串等于他们输入的字符数。我尝试使用“%1s”而不是%c,但这并不能阻止失控的输入 #include<stdio.h> int main() { printf("Welcome.I can predict the future\n" "I learned this gift from someone in

我正在构建一个长程序,需要在if/else语句中使用典型的“y/n”char函数,它可以正常工作,但是如果用户输入了无效的内容,它将重复我输入的“无效答案”字符串,该字符串等于他们输入的字符数。我尝试使用“%1s”而不是%c,但这并不能阻止失控的输入

#include<stdio.h>

int main()
{
 printf("Welcome.I can predict the future\n"
     "I learned this gift from someone in the future.\n"
     "A bright creature with green eyes taught me how.\n"
 "It appeared to me on a Sunday without enthusiam\n"
     "and told me I would end up trapped in a computer,\n"
 "and there was nothing I could do about it.\n"

 "It was really cruel of it to do that.\n"
 "I could have enjoyed the rest of my days\n"
 "without being depressed having known that...\n"
 "I also didn't need to know so many other things I\n"
 "now have learned.\n\n"

 "Having said this, would you like me to predict you\n"
     "future?y/n\n");

 char ansr;
 scanf("%1s", &ansr);


 while (ansr != 'y' && ansr != 'n'){

 printf("Invalid answer, Please try again.");
 scanf("%1s", &ansr);
 }

 if ( ansr == 'y') {
 printf("You've been warned.\n\n");
 }
 else if ( ansr == 'n') {
 printf("Goodbye Then.\n\n");
 }




 return 0;

 }
#包括
int main()
{
printf(“欢迎。我可以预测未来\n”
“我从将来的某个人那里学到了这份礼物。\n”
“一个有着绿色眼睛的明亮生物教我怎么做。\n”
“它在一个没有热情的星期天出现在我面前\n”
“并告诉我我最终会被困在电脑里,\n”
“对此我无能为力。\n”
“这样做真是太残忍了。\n”
“我本可以享受剩下的日子的\n”
“没有因为知道…\n而沮丧”
“我也不需要知道那么多其他事情,我\n”
“现在您已经学会了。\n\n”
说了这些,你想让我预测你吗\n
“未来?是/否\n”);
char ansr;
scanf(“%1s”、&ansr);
而(ansr!='y'&&ansr!='n'){
printf(“无效答案,请重试”);
scanf(“%1s”、&ansr);
}
如果(ansr='y'){
printf(“您已被警告。\n\n”);
}
else if(ansr=='n'){
printf(“那么再见。\n\n”);
}
返回0;
}

首先,您不希望将
%1s
字符一起使用,因为此格式说明符需要一个数组。在
ansr
的内存位置写入1个字符后,它将在下一个内存位置写入一个空字节。这导致了未定义的行为。坚持使用
%c

要清除多余的字符,需要在循环中使用
getchar
读取字符,直到找到换行符。这将清除缓冲区

while (ansr != 'y' && ansr != 'n') {
    printf("Invalid answer, Please try again.");
    while ( getchar() != '\n');
    scanf("%c", &ansr);
}
输出:

。。。
说了这些,你想让我预测你吗
未来?是/否
伪造的
回答无效,请重试。错误
回答无效,请重试。是
你已经被警告过了。

要处理输入中的字符串,您可以尝试按如下方式将stdin读入
ansr
缓冲区,然后进行字符串比较。通过使用
%*[^\n]
丢弃
stdin
中的其余内容,扫描不受空格或多个字符的影响,您可以得到所需的
y/n
字符。另外,不要忘记右大括号,并在
main
中返回零

#include <stdio.h>
#include <string.h>
#define buffLen 32

int main() {        

    char ansr[buffLen] = "";

    printf("...would you like me to predict your future? (y/n) \n");

    while (strcmp(ansr, "y") != 0 && strcmp(ansr, "n") != 0){

        // Read the string, and throw away the rest up to the newline char.
        scanf("%s%*[^\n]", &ansr);

        if (strcmp(ansr, "y") == 0) {
            printf("You've been warned.\n");
        } else if (strcmp(ansr, "n") == 0) {
            printf("Goodbye Then.\n");
        } else {
            printf("Invalid answer, Please try again.\n");
        }
    }

    return 0;
}

charansr[2];扫描频率(“%1s”,ansr);虽然(*ansr!=“y”&&&&*ansr!=“n”){/code>使用:
char-ansr;scanf(“%1s”,&ansr);
是错误的;
%1s
需要一个指向2个字符的指针,一个用于字符,另一个用于字符串末尾的null。可能您需要
字符应答;if(scanf(“%c”,&ansr)==1){…OK…}否则{…EOF或错误…}
。请注意格式字符串中的前导空格以及尾随空格或空白的缺失;这两者都很重要。您可能希望将其转换为do-while循环。如前所述,此程序可能在第一次出现未定义的行为,或者根本无法运行,因为它调用了
strcmp()
在初始化
ansr
的内容之前。opps,很好的捕获,我的意思是初始化它。缓冲区溢出,这个程序有一个很容易被发现的,在scanf调用中,给它一个长度,否则就容易受到攻击。关于溢出的好处,我们可以用一个像
scanf(%31s%*[^\n],&ansr)这样的神奇数字来防止溢出;
这是一个非常有趣的回答,尽管现在我将使用最上面的一个,因为我更好地理解它……您能为我详细说明“%*[^\n]”每个字符的作用吗?
#include <stdio.h>
#include <string.h>
#define buffLen 32

int main() {

    char ansr[strLen];

    printf("...would you like me to predict your future? (y/n) \n");

    do {
        // Read the string, and throw away the rest up to the newline char.
        scanf("%s%*[^\n]", &ansr);

        if (strcmp(ansr, "y") == 0) {
            printf("You've been warned.\n");
        } else if (strcmp(ansr, "n") == 0) {
            printf("Goodbye Then.\n");
        } else {
            printf("Invalid answer, Please try again.\n");
        }
    } while (strcmp(ansr, "y") != 0 && strcmp(ansr, "n") != 0);

    return 0;
}