Mail Archives: djgpp/2001/04/17/14:45:27
On Tue, 17 Apr 2001 16:59:54 +0200, Tim Van Holder
<tim DOT vanholder AT falconsoft DOT be> wrote:
> Michael Ahumibe wrote:
> >
> > Can someone tell me what's wrong? or if there is a work around.
>
> As said before fflush(stdin) isn't supposed to clear the input; I
> believe
> that's a Borlandism (or a Microsoftism, or both).
>
> But IIRC you can use the '%*c' specifier to read any number of
> characters
> (effectively clearing the input), without placing them into memory.
>
> So scanf ("%d%*c", &age) should read '20' into age and discard the rest.
No. Fortunately this isn't true. If %*c would mean "read any number of
characters" it would prompt eternally for input until EOF...
%*c
would read and discard one character only because this syntax is
equivalent to %*1c. And the problem is that it would prompt for input
if no character is available. A correct procedure to throw trailing
input and leave the terminating \n in the stream is
int test;
test=scanf("%d", &age);
scanf("%*[^\n]");
/* throw anything beyond what could be read. Stop
reading and throwing in front of \n */
/* if test == 1 then the input line started with a valid int,
otherwise age's value won't change */
Regards
Horst
- Raw text -