如何修复 - 使用 getchar() 和 do-while 的意外输出
How to fix - unexpected output using getchar() and do-while
我正在尝试制作一个简单的代码,它将从输入中读取一个字符并再次执行 "Correct" 或 "Incorrect input" 和 运行 代码,直到输入正确的输入。首先,它不适用于大写字母 X。我要解决的另一个问题是,在输入错误后,我必须按回车键才能获得 "Enter x" 消息,而不是在错误输入消息后立即进入.
#include <stdio.h>
int main()
{
do
{
printf("Enter x\n");
if (getchar()=='x'|| getchar()=='X')
{
printf("Entered char is X\n");
return 0;
}
else
{
printf("Input incorrect! Please try again!!!\n");
}
}
while (getchar()!='x' || getchar()!='X');
return 0;
}
您需要将输入存储在变量中,否则每次 getchar
调用都会连续多次请求输入。
由于奇怪的历史原因,getchar
实际上 returns 一个 int
,因为可以从它返回的值 EOF
是一个 int
。所以变量必须是int
.
最后,每次用户按下回车键时,一个不可见的换行符 \n
会附加到输入流中。这个角色对你没有好处,所以你应该放弃它并额外阅读。
#include <stdio.h>
int main (void)
{
int input;
do
{
printf("Enter x\n");
input = getchar();
getchar(); // extra getchar to chew up line feed from stdin
if (input=='x'|| input=='X')
{
printf("Entered char is X\n");
}
else
{
printf("Input incorrect! Please try again!!!\n");
}
} while (input!='x' && input!='X');
return 0;
}
请注意 input=='x'|| input=='X'
的反义词是 input!='x' && input!='X'
(德摩根定律)。 "If input is not 'x' and input is not 'X' then loop".
当您按下 ENTER
键时,换行符 \n
被放置在输入缓冲区中。您需要使用该换行符才能阅读下一个字符。
此外,您正在阅读两次,在这种情况下这是不必要的。所以你的代码应该是这样的
#include <stdio.h>
int main()
{
char inp;
do
{
printf("Enter x\n");
inp = getchar();
getchar(); // reading the newline character '\n'
if (inp == 'x'|| inp =='X')
{
printf("Entered char is X\n");
return 0;
}
else
{
printf("Input incorrect! Please try again!!!\n");
}
}
while (inp !='x' || inp !='X');
return 0;
}
p.s There is no need to put condition checking in while loop, since you are returning in if condition. while(true)
would work fine. Thanks @bruno for pointing that out.
在您的代码中:
if (getchar()=='x'|| getchar()=='X')
getchar() 被调用了两次。
相反,你应该这样写:
char c = getchar();
if (c=='x'|| c=='X')
对于第二部分,如果您的目标是在新行上打印消息,那么只需将您的 printf 更改为:
printf("\nInput incorrect! Please try again!!!\n");
我正在尝试制作一个简单的代码,它将从输入中读取一个字符并再次执行 "Correct" 或 "Incorrect input" 和 运行 代码,直到输入正确的输入。首先,它不适用于大写字母 X。我要解决的另一个问题是,在输入错误后,我必须按回车键才能获得 "Enter x" 消息,而不是在错误输入消息后立即进入.
#include <stdio.h>
int main()
{
do
{
printf("Enter x\n");
if (getchar()=='x'|| getchar()=='X')
{
printf("Entered char is X\n");
return 0;
}
else
{
printf("Input incorrect! Please try again!!!\n");
}
}
while (getchar()!='x' || getchar()!='X');
return 0;
}
您需要将输入存储在变量中,否则每次 getchar
调用都会连续多次请求输入。
由于奇怪的历史原因,getchar
实际上 returns 一个 int
,因为可以从它返回的值 EOF
是一个 int
。所以变量必须是int
.
最后,每次用户按下回车键时,一个不可见的换行符 \n
会附加到输入流中。这个角色对你没有好处,所以你应该放弃它并额外阅读。
#include <stdio.h>
int main (void)
{
int input;
do
{
printf("Enter x\n");
input = getchar();
getchar(); // extra getchar to chew up line feed from stdin
if (input=='x'|| input=='X')
{
printf("Entered char is X\n");
}
else
{
printf("Input incorrect! Please try again!!!\n");
}
} while (input!='x' && input!='X');
return 0;
}
请注意 input=='x'|| input=='X'
的反义词是 input!='x' && input!='X'
(德摩根定律)。 "If input is not 'x' and input is not 'X' then loop".
当您按下 ENTER
键时,换行符 \n
被放置在输入缓冲区中。您需要使用该换行符才能阅读下一个字符。
此外,您正在阅读两次,在这种情况下这是不必要的。所以你的代码应该是这样的
#include <stdio.h>
int main()
{
char inp;
do
{
printf("Enter x\n");
inp = getchar();
getchar(); // reading the newline character '\n'
if (inp == 'x'|| inp =='X')
{
printf("Entered char is X\n");
return 0;
}
else
{
printf("Input incorrect! Please try again!!!\n");
}
}
while (inp !='x' || inp !='X');
return 0;
}
p.s There is no need to put condition checking in while loop, since you are returning in if condition.
while(true)
would work fine. Thanks @bruno for pointing that out.
在您的代码中:
if (getchar()=='x'|| getchar()=='X')
getchar() 被调用了两次。
相反,你应该这样写:
char c = getchar();
if (c=='x'|| c=='X')
对于第二部分,如果您的目标是在新行上打印消息,那么只需将您的 printf 更改为:
printf("\nInput incorrect! Please try again!!!\n");