C-循环中的getchar()? [英] C - getchar() in a loop?

查看:174
本文介绍了C-循环中的getchar()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在循环中使用getchar()?现在我有...

How I can use getchar() in a loop? Now I have...

for (p=0; p<n_players; p++) {
    ...
    fflush(stdin);
    getchar();
}

但是它不起作用...如果n_players为3,它只会在最后执行两次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跳到第一个循环

getchar jumps the first loop

推荐答案

首先,刷新输入流的结果是不确定的.其次,行不通"并不能给我们带来很多好处.

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

这篇关于C-循环中的getchar()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆