gpt4 book ai didi

c - 在 C 中 - for 循环中的 scanf() 导致 printf() 运行多次

转载 作者:行者123 更新时间:2023-11-30 16:51:31 25 4
gpt4 key购买 nike

我正在完成一项作业,完成后,我有 1 个错误,并且修复了 1 个我不完全理解的错误。目前,只要用户按照要求进行操作,一切就可以正常工作。但我知道这种情况并不经常发生,所以我很想知道如何阻止这些问题。希望得到任何建议 - 我是 C 语言的初学者。

我在这里找到了许多不同的建议:C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf

我在 scanf() 语句中添加了一个空格,这解决了一些错误 - 我知道\n 被添加到输入的字符串/字符的末尾,我只是不确定如何检查它/处理它,我尝试使用 getchar() 代替 scanf() 但仍然遇到双重打印/循环问题。

错误问题

当用户运行游戏循环时,如果输入超过 1 个字符(例如:“oo”,当使用 scanf() 提示输入“y”或“n”时),我的 printf 语句会运行 1x每个输入的字符,并相互连接:

示例是:

Welcome to Two doors.

Would you like to play? (y/n):Welcome to Two doors.

Would you like to play? (y/n):

如果用户输入“y”来玩游戏,但随后在第二部分中输入 1,2 或 3 以外的字符,也会出现此问题。

我如何限制他们的回复长度?或者是在输入 if 语句之前监视游戏长度和选择变量的最佳方法?也许检查它们是否长于 1 个字符,如果是,则只取第一个字符?

第二个问题 - 修复我不明白的错误在 scanf() 函数中,我遇到了与上面描述的非常相似的问题,但当用户输入任何字符时就会发生这种情况。我找到的解决方案是在字符前添加一个空格 ->

scanf(" %c", &play);

对比

scanf("%c", &play);

这个问题只有在使用循环时才会出现吗?因为在循环代码之前我从未发现这些错误。

使用 'while (getchar() != '\n');' 更新了代码Sourav Ghosh 的建议

#include <stdio.h>

int main(void) {

char play;
int choice;
char answer[] = "No matter which one you choose the guards both tell you which door leads to death, and therefore you can pick the other door.\n";
int gameLoop = 1;
int timesPlayed = 0;

while (gameLoop == 1){

if (timesPlayed == 0) {
printf("Welcome to Two doors.\n");
printf("Would you like to play? (y/n):");
} else {
printf("Would you like to play again? (y/n):");
}
scanf(" %c", &play);
while (getchar() != '\n');

if (play == 'y') {
// == instead of =
printf("\nYou are a prisoner in a room with 2 doors and 2 guards.\n");
printf("One of the doors will guide you to freedom and behind the other is a hangman --you don't know which is which.\n");
printf("One of the guards always tells the truth and the other always lies. You don't know which one is the truth-teller or the liar either.\n");
printf("You have to choose and open one of these doors, but you can only ask a single question to one of the guards.\n");
printf("What do you ask so you can pick the door to freedom?\n\n");
printf("\t1.Ask the truth-guard to point to the door of doom.\n");
printf("\t2.Ask the liar-guard to point to the door of doom.\n");
printf("\t3.Doesn't matter which one you pick.\n");
scanf(" %d", &choice);
while (getchar() != '\n');

switch (choice) {

case 1:
printf("%s", answer);
timesPlayed++;
break;

case 2:
printf("%s", answer);
timesPlayed++;
break;

case 3:
printf("%s", answer);
timesPlayed++;
break;

default:
printf("The Troll Smasher comes out from the shadows and squeezes the stupid out of you until you pop. GAME OVER!\n");
break;
}
} else if(play == 'n') {
printf("Sorry to hear that, we at Two Doors hope you have a super duper day!\n");
gameLoop = 0;
break;
} else {
printf("That is not a valid input, please try again by entering either 'y' to start the game or 'n' to quit the game.\n");
}
}
return 0;

}

最佳答案

%c 格式说明符的问题是,它只会从输入缓冲区读取一个字节,并且如果输入缓冲区存储了更多字节并且调用下次遇到时,它不会要求用户输入,它只会从可用输入流中读取下一个字节。

那么,回答一下

How can I limit the length of their response?

嗯,没有直接的方法可以阻止用户只输入 X 个字符/数字,而是刷掉多余的字符/数字(如果有)并下一次调用时,从缓冲区开始是一种简单的方法。

因此,解决这个问题的快速方法是清除剩余输入的标准输入。你可以做类似的事情

  int retval = scanf(" %c", &play);

//some code

while (getchar() != '\n'); //eat up the input buffer

//next call to scanf(), input buffer is empty now....

阻止 scanf() 读取已存在的不需要的输入,并强制它询问用户输入。

另外,不要忘记检查 scanf() 的返回值以确保调用成功。

关于c - 在 C 中 - for 循环中的 scanf() 导致 printf() 运行多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41734880/

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