1

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

2
  • 1
    swap the fflush(stdin) and getchar() calls. Yes, fflush(stdin) is technically undefined, but it does the correct thing in both ms-cl and gcc Commented Jan 6, 2010 at 17:22
  • 1
    What "does not work" ? What behavior are you seeing and what are you expecting ? Keep in mind that getchar() might not process anything until you hit the enter key. If n_players is 3, the loop will run 2 times. Commented Jan 6, 2010 at 17:47

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

The more specific you are, the more people will be inclined to help you.
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.

Comments

3

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)

2 Comments

c = getchar(); getchar(); At the first loop it's ok... but at the second I must press twice a button :(
keep in mind that, in order to make getchar work the way you want, you must press a letter before hitting enter. just hitting ENTER won't work with this trick.
3

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.