scanf 在 C 中不能正常工作 [英] scanf not working well in C
问题描述
if (a % 5)
goto ask;
else
goto main;
ask:printf("Do You Want To Exit ? Y \\ N . . . \n");
scanf("%c", &YN);
getch();
if (YN == 'Y')
{
y: system("cls");
YN = 1;
goto sign;
}
else if (YN == 'y')
goto y;
else if (YN == 'N')
{
n: system("cls");
YN = 0;
goto sign;
}
else if (YN == 'n')
{
goto n;
}
else
{
printf("Sorry ..Didn't Catch that ... ");
goto ask;
}
有人帮助我了解我的问题出于某种原因,我从这段代码中得到的输出是你想退出 y\n 吗?"获取...抱歉没听清你想退出 y\n 吗?"
someone help me understand my problem for somereason the output I get from this code is "Do u want to exit y\n ?" getchar ... "sorry didnt catch that do u want to exit y\n ? "
好像是第一次跳过了scanf()
,程序直接跳到了else
==> 抱歉我没搞定那个"
并且只有在第二次才知道如何使用 scanf()
.
It looks like it jumped over the scanf()
for the first time and the program went directly to the else
==> "sorry i didnt get that"
and only in the second time it fiugres out how to use the scanf()
.
推荐答案
scanf()
使用 %c
读取字符,是的,ENTER 按键 [在您之前的输入之后] 对于 %c
几乎有效 [检查下面的剧透].
scanf()
reads characters with %c
and yes, the ENTER key press [after your previous input] is pretty much vaild for %c
[Check the below spoiler].
ENTER 键按下 == 换行
ENTER key press == newline
使用
scanf(" %c", &YN); //mind the space brefore `%c`
^
|
忽略任何先前存储的[也,前导]空格[包括换行符.]
to ignore any previously-stored [also, leading] whitespace [including a newline.]
注意:这也消除了对 getch();
这篇关于scanf 在 C 中不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!