【发布时间】:2016-04-09 04:17:13
【问题描述】:
我有这个循环,但是当我在我的角色之后按回车键时,它会对其进行处理,然后在再次要求输入之前处理'\n'。请!!!!帮助
int input;
while (true){
input = getchar();
fflush(NULL);
input = input - '0';
if( input != 'e' && input != '\n') {
rc = state_fun(input);
}
5[ENTER] 处理 5 作为输入,然后处理 10(即 ascii '\n')作为输入,然后再次请求输入。快把我逼疯了
【问题讨论】:
-
在 while 循环的末尾添加一个虚拟 getchar() 以使用该 '\n'
-
刷新标准输入的便携方式是
int c; while((c = getchar()) != '\n' && c != EOF);。fflush(NULL)不会刷新stdin,因为标准说fflush(stdin);是未定义的行为。 -
您刚刚从
input中减去了'0'。之后,您针对e或\n测试该结果值,因此如果您按:,它将使条件为真 -
您使用的是什么操作系统?