www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/18/09:40:19

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: TCP/IP Library, Svasync Library, and a Stdout that won't flush
Date: Thu, 13 Feb 1997 00:40:18 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 68
Message-ID: <3302D372.763C@cs.com>
References: <32FE32DF DOT 0 AT cyberspace DOT com> <5dr5ki$8qs AT flex DOT uunet DOT pipex DOT com> <33013F95 DOT 5F30 AT juno DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp106.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Logan Bowers wrote:
> 
> The coputer doesn't wait, but continues on even though I flushed the
> stdin.  Is there any other function I need call, or some little detail
> I'm missing?  Thanks.

You are not allowed to flush stdin; it's an input stream and fflush() is
only defined for output streams.  That it works under Borland is a sign
of that company's willingness to violate (ok, "extend") the ANSI
standard.

There are several techniques that work to ensure that each input
statement gets exactly what it expects.  The easiest is as simple as
this:

  while( getchar() != '\n' );

This should be used after any input statement (scanf and getchar in
particular) that are guaranteed to leave a newline in the buffer.  Of
course, it isn't foolproof.  The ONLY totally foolproof way to get input
one line at a time is to use the function that was designed for the
purpose:  gets().  Example:

/* Gets two numbers from the user.  Returns 1 on success, 0 if blank was
entered, and -1 on error */
int getnums( int &pn1, int &pn2 )
{
    char line[256];

    while ( 1 )
    {
        printf( "Enter two numbers [blank to quit]:  " );
        if ( gets( line ) == NULL )	/* EOF */
        {
            fprintf( stderr, "getnums:  unexpected EOF\n" );
            return -1;
        }
        else if ( strlen( line ) == 0 )	/* blank line */
            return 0;
        else if ( sscanf( line, "%d%d", pn1, pn2 ) == 2 )
            break;
        else
            printf( "Please enter exactly two numbers!\n" );
    }

    return 1;
}

Of course, many people will tell you that gets() is inherently unsafe
because it doesn't check the maximum length of the string that is passed
to it, possibly allowing memory to be overwritten if stdin is redirected
to a large file.  One solution to this is to use fgets(), and perform
some parsing to determine if the newline was in fact recorded or not. 
It's not difficult, but beyond the scope of this answer.  :)  If you
want an example, email me and I'll send you one.

The final alternative is to use the conio functions to get input
directly from the BIOS.  This requires that you understand a bit of how
your computer works, but can have the best results overall.  If you want
to use conio functions in your programs, I suggest you read the libc
documentation.

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I  |        fighteer AT cs DOT com         |
| Plan:  To find ANYONE willing to |   http://www.cs.com/fighteer   |
| play Descent 2 on DWANGO!        | Tagline: <this space for rent> |
---------------------------------------------------------------------

- Raw text -


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