From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: flushing buffers Date: Sun, 24 Nov 1996 00:55:31 -0800 Organization: Three pounds of chaos and a pinch of salt Lines: 67 Message-ID: <32980D83.39F8@cs.com> References: <3297FE31 DOT 34F5 AT freenet DOT carleton DOT ca> Reply-To: fighteer AT cs DOT com NNTP-Posting-Host: ppp102.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: "J. Lee" To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp J. Lee wrote: > > I am using DJGPP 2.01, running Windows 3.1 with 16megs of ram. > > My problem is as follows. > > Before i get an input from the keyboard using any of the stdio functions > (ex. scanf, getchar(),gets etc.), i always flush out the buffer using > fflush(stdin). However, the buffer is never flushed out. On my next > getchar() statement, for example, the remaining characters in the buffer > are retrieved instead of waiting for new input. The fflush(stdin) work > fine when i am using UNIX. How can i solve this very frustrating > problem in DJGPP? According to the ANSI spec, fflush() is not defined when used on input streams. Thus, the behavior of Unix compilers (and Borland, et. al.) is nonstandard and cannot be relied upon in portable code. It's just plain bad programming. Fortunately, you have several solutions besides using fflush(): * You can read in an extra character after the input to capture the newline as well. This is easy, but doesn't account for appended text, spaces, or anything else. scanf( "%d%*c", &num ); /* read one extra character and ignore it */ * You can use a loop to retrieve all remaining characters until a newline is pressed. Not 100% safe, but will work in most cases. scanf( "%d", &num ); while ( getchar() != '\n' ) ; * You can read in a line at a time and process the output therefrom. (Using gets() in this fashion can be unsafe if input to the program is redirected; fgets() is a better but more complex solution.) char line[255]; gets( line ); sscanf( line, "%d", &num ); * You can read in one character at a time and discard any non-number ones. This is very safe, but doesn't permit error-checking; i.e., if your user enters "055x4~Z2", you'll get 05542. Further refinements can easily fix this, of course. char line[255]; char c; int i; for ( i = 0; ( c = getchar() ) != '\n'; ) if ( isdigit( c ) && i < 255 ) line[i++] = c; line[i++] = '\0'; /* very important */ num = atoi( line ); /* atof, atol, atold, etc., or just sscanf */ * You can use conio functions to read input directly from the keyboard. This can be complex, so I'm not going to provide an example that might not be correct. ;) -- --------------------------------------------------------------------- | John M. Aldrich, aka Fighteer I | fighteer AT cs DOT com | | Proud owner of what might one | http://www.cs.com/fighteer | | day be a spectacular MUD... | Plan: To make Bill Gates suffer | ---------------------------------------------------------------------