C - getchar() 在循环中?

发布于 2024-08-17 04:07:04 字数 866 浏览 5 评论 0原文

如何在循环中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

金橙橙 2024-08-24 04:07:04

首先,刷新输入流的结果是未定义的。其次,“行不通”并没有给我们很多继续下去的机会。

Firstly, the result of flushing an input stream is undefined. Secondly, "doesn't work" does not give us a lot to go on.

习ぎ惯性依靠 2024-08-24 04:07:04

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.

夏日浅笑〃 2024-08-24 04:07:04

getchar() 不是处理用户输入的好选择。话虽如此,如果您仍然想使用该函数,您可以尝试不使用 fflush 并堆积两次对 getchar 的调用:

像这样:

for (p=0; p<n_players; p++) {
   ...
   c = getchar(); // c will hold character read
   getchar(); // will consume '\n'
}

带有 < 的东西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 using fflush and piling up two calls to getchar:

Something like this:

for (p=0; p<n_players; p++) {
   ...
   c = getchar(); // c will hold character read
   getchar(); // will consume '\n'
}

The thing with getchar() is that it returns next character available in the keyboard buffer. So, if you do a c = getchar() and user does:

E'\n'

(meaning he/she presses character E followed by ENTER)

c will hold value 'E' and the next call to getchar() 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)

云朵有点甜 2024-08-24 04:07:04

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文