scanf无法处理无效的输入 [英] scanf not working on invalid input
问题描述
在第一个 scanf()
中输入字符时,第二个字符不运行。 getchar()
不能用于重试输入。跳过输入您是否想再次播放? (是/否)?看来 your_choice
应该是字符,然后再检查,但是字符实际上是由 ch
。是什么导致它像这样工作以及如何解决问题。我试过重新初始化变量,但不起作用。
On a character input in the first scanf()
, the second one doesn't run. getchar()
isn't working either for Try Again input. It skips to take input for Would you like to play again? (Y/N)? It seems that your_choice
is supposed to take the character and check it afterward but the character is actually being taken by ch
. What is causing it to work like this and how to resolve the issue. I've tried re-initializing the variables but doesn't work.
#include <stdio.h>
void choice(int);
int main() {
char ch;
int random, your_choice;
do {
srand(time(NULL));
system("cls");
printf("** 0 is for Rock **\n");
printf("** 1 is for Scissors **\n");
printf("** 2 is for Lizard **\n");
printf("** 3 is for Paper **\n");
printf("** 4 is for Spock **\n");
printf("\nEnter your choice here:");
scanf("%d", &your_choice);
random = rand() % 5; //random number between 0 & 4
if ((your_choice >= 0) && (your_choice <= 4)) {
//choice printer omitted for this post
if ((random == ((your_choice + 1) % 5)) || (random == ((your_choice + 2) % 5)))
printf("\n\n... and you win!!!\n");
else if ((random == ((your_choice + 3) % 5)) || (random == ((your_choice + 4) % 5)))
printf("\n\n... and you lose!!!\n");
else if (random == your_choice)
printf("\n\nUnfortunately, it's a tie!\n");
} else
printf("\nWell, this is wrong! Try again with a number from 0 to 4!!\n");
printf("\nWould you like to play again? (Y/N)?: ");
scanf(" %c", &ch);
} while (ch == 'y' || ch == 'Y');
return 0;
}
推荐答案
如果用户输入的字符无法转换为数字, scanf(%d,& your_choice);
返回0,而 your_choice
为保持不变,因此未初始化。行为是不确定的。
If the user enters characters that cannot be converted to a number, scanf("%d", &your_choice);
returns 0 and your_choice
is left unmodified, so it is uninitialized. The behavior is undefined.
您应该对此进行测试并以这种方式跳过有问题的输入:
You should test for this and skip the offending input this way:
if (scanf("%d", &your_choice) != 1) {
int c;
/* read and ignore the rest of the line */
while ((c = getchar()) != EOF && c != '\n')
continue;
if (c == EOF) {
/* premature end of file */
return 1;
}
your_choice = -1;
}
说明:
-
scanf()
返回成功的转换次数。如果用户键入数字,则将其转换并存储到your_choice
中,如果用户输入scanf()
,则返回1输入不是数字的内容,例如AA
,scanf()
将有害的输入保留在标准输入缓冲区中并返回0,最后如果到达文件末尾(用户在Windows中输入^ Z输入或在UNIX中输入^ D),scanf()
返回EOF
。
scanf()
returns the number of successful conversions. If the user types a number, it is converted and stored intoyour_choice
andscanf()
returns 1, if the user enters something that is not a number, such asAA
,scanf()
leaves the offending input in the standard input buffer and returns 0, finally if the end of file is reached (the user types ^Z enter in windows or ^D in unix),scanf()
returnsEOF
.
如果输入未转换为数字,则输入的正文if
语句:使用 getchar()
一次消耗一个字节的输入,直到读取文件末尾或换行符为止。
if the input was not converted to a number, we enter the body of the if
statement: input is consumed one byte at a time with getchar()
, until either the end of file or a linefeed is read.
如果 getchar()
返回 EOF
,我们已经读取了整个输入流,无需提示用户进行更多输入,您可能需要在返回错误代码之前输出错误消息。
if getchar()
returned EOF
, we have read the entire input stream, no need to prompt the user for more input, you might want to output an error message before returning an error code.
否则,将 your_choice
设置为 -1
,该值无效,因此读取
otherwise, set your_choice
to -1
, an invalid value so the read of the code complains and prompts for further input.
必须读取并丢弃有问题的输入:如果您不这样做,这样做,下一个输入语句 scanf(%c,& ch);
将读取有问题的输入的第一个字符,而不是等待用户输入以响应您想再玩一次吗? (是/否)?:
提示。这是您观察到的行为的解释。
Reading and discarding the offending input is necessary: if you do not do that, the next input statement scanf(" %c", &ch);
would read the first character of the offending input instead of waiting for user input in response to the Would you like to play again? (Y/N)?:
prompt. This is the explanation for the behavior you observe.
这篇关于scanf无法处理无效的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!