cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/26/15:15:48

From: drathj01 AT xlab1 DOT fiu DOT edu (daniel k rathjens)
Newsgroups: comp.lang.c++,comp.os.msdos.djgpp
Subject: Re: EOF
Followup-To: comp.lang.c++,comp.os.msdos.djgpp
Date: 26 Jan 1997 08:25:58 GMT
Organization: Florida International University
Lines: 32
Message-ID: <5cf4em$dca@isis.fiu.edu>
References: <32E6383C DOT 750D AT cu DOT lu>
NNTP-Posting-Host: xlab1.fiu.edu
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I had the same problem a few days ago.  The part I missed at first was
the same thing I think you have.  When the attempt is made to read the
^Z a few things happen:  an error flag is turned on and cin goes into
a failed state, the loop is skipped, and the value of i is _not_ changed
i not being changed means that the ^Z is still sitting there in the 
stream since it hasn't been read yet. and using the clear() function
does not clear the stream as it's name would imply, it simply puts
the stream back into a non-failed state.  So, when you call cin >> d;
now it attempts to read that ^Z again, and goes back into a failed
state again because it's not a double, so you're still left with the
^Z in the stream.  Then your output of d is unpredictable because
nothing has been read into it.  The solution is to add 
: #include <iostream.h>
: int main() {
:   int i;
:   while(cin >> i) cout << i << endl;  // CTRL Z to stop the loop
:   cin.clear();                        // reset the flags    
    char ch;
    cin.get(ch);
:   double d;
:   cin >> d;                           // with DJGPP gcc, the user cannot
: enter a value 
:   cout << d << endl;
: }

Which brings us to my question.  Is there a better way to empty the
stream than:  while(cin.get(ch) { }?
--
#include <disclaimer.h>    #include <humorous_quote.h>
void main(geek *code) {    // pointer points to: Danny Rathjens
GCS/M/S d+(-) s+:- a-- C+++>$ ULSC++>+++$ P+? L++ E W++(+) N+++ o K w++ 
!O !M- V- PS PE+ Y-- PGP- t++@ !5 X+ R+ tv b+++ DI+ D+ G++ e+ h r y? }

- Raw text -


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