www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/04/08/17:00:10

From: Jack Klein <jackklein AT spamcop DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: fflush (in djgpp)
Message-ID: <82j1dtc5m6lg195fo29ddc7r19vrp4v0k7@4ax.com>
References: <9aqfuu$fnb$1 AT uranium DOT btinternet DOT com>
X-Newsreader: Forte Agent 1.8/32.548
MIME-Version: 1.0
Lines: 95
Date: Sun, 08 Apr 2001 20:49:13 GMT
NNTP-Posting-Host: 12.75.161.56
X-Complaints-To: abuse AT worldnet DOT att DOT net
X-Trace: bgtnsc04-news.ops.worldnet.att.net 986762953 12.75.161.56 (Sun, 08 Apr 2001 20:49:13 GMT)
NNTP-Posting-Date: Sun, 08 Apr 2001 20:49:13 GMT
Organization: AT&T Worldnet
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sun, 8 Apr 2001 20:58:06 +0100, "Michael Ahumibe"
<M DOT Ahumibe AT btinternet DOT com> wrote in comp.os.msdos.djgpp:

> Hi
> 
> I apologise in advance to those who saw this in comp.lang.c. It wasn't meant
> for that news group but was meant,
> for this one, but was posted in error.
> I'm having problems with the fflush command. Every time I use it, doesn't
> work.

Terminology is important!  fflush() is not a command.  There are no
commands in C.  fflush() is a function.

> Ok, here's an example of me using the fflush command...
> 
>  /* Clearing stdin of extra characters. */

What makes you think that fflush() is SUPPOSED to clear stdin of extra
characters?

>  /* Using the fflush() function         */
>  #include <stdio.h>
> 
>  main()
>  {
>     int age;
>     char name[20];
> 
>     /* Prompt for user's age. */
>     puts("Enter your age.");
>     scanf("%d", &age);
> 
>     /* Clear stdin of any extra characters. */
>     fflush(stdin);

You have a misconception here.  The ANSI/ISO standard which defines
the C programming language states that the fflush() function is used
on output streams or update streams where the last operation was an
output.  Using it on an input stream like stdin is undefined behavior.

The purpose of the fflush() function is to cause to the C stream
functions to pass any buffered output data that they might have been
holding internally on to the operating system.  Nothing in its
definition implies that it is supposed to clear or throw away
anything.

> 
>     /* Now prompt for user's name. */
>     puts("Enter your first name.");
>     scanf("%s", name);
> 
>     /* Display the data. */
>     printf("Your age is %d.\n", age);
>     printf("Your name is %s.\n", name);
> 
>     return 0;
> }
> 
> Output example...
> 
> Enter your age.
> 20 years old (my input)
> Enter your first name.
> Your age is 20.
> Your name is years.
> 
> Can someone tell me what's wrong? or if there is a work around.
> 
> thanks
> Mike

The real work around is to never, never use scanf() for interactive
user input.  The function is poorly designed in the first place, and
it can be tricky to use even when you know the exact format of the
data it will be receiving, but it is virtually impossible with real
users who have a tendency not to follow the exact format you had in
mind when you wrote your code.

If you think I am exaggerating, run the program that you have and
enter "xyz" at the first prompt for age and see what happens.  Even on
some systems that define a non-standard extension to fflush() that
does what you think you want it to do, it does not fix a problem like
this.

Start by looking at the FAQ for comp.lang.c (link in my signature) for
better methods of handling input, such as the fgets() function.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019