C – getchar() in a loop

cgetcharloopsstdin

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

Best Answer

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