C - getchar() 在循环中?
如何在循环中使用 getchar() ?现在我有...
for (p=0; p<n_players; p++) {
...
fflush(stdin);
getchar();
}
但它不起作用...如果 n_players 是 3,它只在最后执行 getchar 2 次...
for (p=0; p<n_players; p++) {
blank_start();
ascii_art_title();
printf("%s, tocca a te...\n",player_info[p].player_name);
srand(time(NULL));
random_speed = MIN_WHEEL_SPEED + rand()%MAX_WHEEL_SPEED;
move_wheel_pointer(random_speed, &pointer);
if (player_points(&wheel[pointer]) == 0){
player_info[p].points = wheel[pointer];
}
else {
player_info[p].points = 0;
}
printf("\nGuadagni %d punti...\n",player_info[p].points);
if (p<(n_players-1)) {
printf("\nOra tocca a te, giocatore %d\n",(p+2));
}
fflush(stdin);
getchar();
}
getchar 跳转第一个循环
How I can use getchar() in a loop? Now I have...
for (p=0; p<n_players; p++) {
...
fflush(stdin);
getchar();
}
But it doesn't work... if n_players is 3, it execute getchar 2 times only at the end...
for (p=0; p<n_players; p++) {
blank_start();
ascii_art_title();
printf("%s, tocca a te...\n",player_info[p].player_name);
srand(time(NULL));
random_speed = MIN_WHEEL_SPEED + rand()%MAX_WHEEL_SPEED;
move_wheel_pointer(random_speed, &pointer);
if (player_points(&wheel[pointer]) == 0){
player_info[p].points = wheel[pointer];
}
else {
player_info[p].points = 0;
}
printf("\nGuadagni %d punti...\n",player_info[p].points);
if (p<(n_players-1)) {
printf("\nOra tocca a te, giocatore %d\n",(p+2));
}
fflush(stdin);
getchar();
}
getchar jumps the first loop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,刷新输入流的结果是未定义的。其次,“行不通”并没有给我们很多继续下去的机会。
Firstly, the result of flushing an input stream is undefined. Secondly, "doesn't work" does not give us a lot to go on.
fflush
的行为不是在输入流上定义的,因此所提供的代码是无意义的。如果 n_players 为 3,则该循环确实会发生 3 次。
fflush
's behavior is not defined on an input stream, so the code as presented is nonsensical.That loop will indeed happen 3 times if n_players is 3.
getchar()
不是处理用户输入的好选择。话虽如此,如果您仍然想使用该函数,您可以尝试不使用 fflush 并堆积两次对 getchar 的调用:像这样:
带有 < 的东西code>getchar() 的优点是它返回键盘缓冲区中可用的下一个字符。因此,如果您执行
c = getchar()
并且用户执行:E'\n'
(意味着他/她按下字符 E,然后按 ENTER),
c
将保存值'E' 和下一次调用getchar()
将消耗用户按下的 ENTER ('\n')。因此,正如您所看到的,它非常棘手且难以正确控制。
如果是为了测试一些代码,好的。如果是真实的应用程序,请尝试使用平台相关的库来进行用户输入(Windows 上的 Win32、Linux 上的 GTK、Linux 上的 ncurser 等)
getchar()
is not a good option to process user input. Having said that, if you still want to use that function, you can try by not usingfflush
and piling up two calls togetchar
:Something like this:
The thing with
getchar()
is that it returns next character available in the keyboard buffer. So, if you do ac = getchar()
and user does:E'\n'
(meaning he/she presses character E followed by ENTER)
c
will hold value 'E' and the next call togetchar()
will consume the ENTER ('\n') pressed by user.So, as you can see, it's pretty tricky and hard to control properly.
If it is for testing some code, OK. If it is for a real application, try using platform dependent libraries to do user input (Win32 on Windows, GTK on Linux, ncurser on Linux, etc)
1] 输入流上的 fflush 是未定义的行为。
2]你的循环确实执行了3次。第二次调用 getchar() 将使用第一次输入时放入的流中的
ENTER
键。因此你认为它只被调用两次。简而言之,再放一个 getchar() 来消耗
\n
。那会解决你的问题。1] fflush's on input stream is undefined behavior.
2] Your loop is indeed executed 3 times. The second call to getchar() will consume the
ENTER
key from the stream that was put there the first time input was taken. Hence you think its getting called two times only.Inshort, put one more getchar() to consume the
\n
. That will solve your problem.