learning c
Guy Harris
guy at auspex.auspex.com
Tue Apr 4 19:24:31 AEST 1989
> I know your problem well. What you are looking for is a 'getch()' or
>'getche()' function in place of the 'getchar()' function.
Huh? What would those functions do? They're not in the ANSI C standad
nor are they in any UNIX C library. If they're a PCism, note that
plenty of C programs have never programmed on a PC and, as such, may not
be familiar with them.
>Barring that try
>
>c = getchar();
>fflush(stdin);
>
> The variable 'c' will get the first character of input, and the remainder
>of the line (plus the newline) will get flushed away.
Bad idea, as Rahul Dhesi has already pointed out:
1) "fflush(stdin)" is not guaranteed to flush input on all
implementations;
2) even if it does do so on your implementation, it may or may
not stop at the end of the line in all cases.
Much better to *read* the rest of the line, as in Rahul's posting. Not
only is it portable, it's the right thing to do - flushing input is
generally an action taken on an error, not something you do as a matter
of course unless you want to make *sure* somebody hasn't typed something
ahead that they'll regret (cf. "rn", which does flush input for
precisely that reason).
More information about the Comp.lang.c
mailing list