Article View: comp.lang.c.moderated
Article #2607Re: fflush(stdin) - not guaranteed to work?
From: The Amorphous Mass
Date: Wed, 17 Apr 1996 15:44
Date: Wed, 17 Apr 1996 15:44
29 lines
1873 bytes
1873 bytes
On 16 Apr 1996, News User Id wrote:
> To the implicit followon question, "If
> fflush(stdin) won't work, how *can* I flush the user's typeahead
> before issuing a prompt confirming whether I should reformat the
> hard drive?", the FAQ list suggests no answer, because I don't
> know a good one (other than, your program probably shouldn't be
> offering to do that at all).
Since you offer answers to questions that might work on most
implementations (eg the definition of the offsetof() macro) you might
suggest a variation on
void Flush()
{
while(getchar() != '\n')
;
}
which is only called if the last character read was not a newline.
This has yet to fail on the systems I've tested it on, but that obviously
doesn't mean that it always works. Can anyone get it to fail to
transparently clear stdin (by, say, requuiring that the user hit <CR>)?
Remember to only call it if the last character read was not a newline. :-)
--
James Robinson "Because we are returning a copy for postfix ++
james-robinson@uiowa.edu expressions, statements such as (c++)++; won't
robinson@cs.uiowa.edu work as expected." -- Weiskamp & Flamig,
_The_Complete_C++_Primer_, 2nd ed.
Technical Details
Message-ID:
<4l33ka$7n0@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4ksjpn$rjt@solutions.solon.com> <4ku3id$53h@solutions.solon.com> <4l1idn$pvs@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: The Amorphous Mass <robinson@blue.weeg.uiowa.edu> Newsgroups: comp.lang.c.moderated,comp.lang.c Subject: Re: fflush(stdin) - not guaranteed to work? Date: 17 Apr 1996 10:44:10 -0500 Organization: University of Iowa, Iowa City, IA, USA Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4l33ka$7n0@solutions.solon.com> References: <4ksjpn$rjt@solutions.solon.com> <4ku3id$53h@solutions.solon.com> <4l1idn$pvs@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com X-Sender: robinson@green.weeg.uiowa.edu