Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11523

Re: Using cin.get() and cin.eof() ends up in an endless loop

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: Using cin.get() and cin.eof() ends up in an endless loop
Date 2011-11-16 13:11 -0500
Organization A noiseless patient Spider
Message-ID <ja0uch$cfv$1@dont-email.me> (permalink)
References <ab6230fc-c124-4ad3-b7d8-6e7d4847466a@o17g2000yqa.googlegroups.com> <ja0le0$b4t$1@dont-email.me> <48c5a028-61c1-4cff-ab76-bf44d5869dd8@f29g2000yqa.googlegroups.com>

Show all headers | View raw


On 11/16/2011 11:37 AM, Fernando wrote:
> On 16 nov, 16:38, Victor Bazarov<v.baza...@comcast.invalid>  wrote:
>> Have you read the FAQ?  Section 15 seems exactly what you need.
>
> Thanks a lot for your hint. I had not read it. After doing it, it
> seems question 15.2 is the most relevant in my problem:
>
> [15.2] Why does my program go into an infinite loop when someone
> enters an invalid input character? (http://www.parashift.com/c++-faq-
> lite/input-output.html#faq-15.2)
>
> Si, I thought something like this should work:
>
> #include<iostream>
>
> using namespace std;
>
> int main () {
>      char buffer[1024];
>
>      while (cin.get(buffer, 1024)) {
>          cout<<  buffer;
>      }
>
>      return 0;
> }
>
> because I am not reading integers or anything else that can be non-
> valid. But it only reads the first line of the standard input. It
> seems, somehow, that cin.get() fails in the next line.

I am uncertain what you were expecting here.  If the standard input 
contains less than 1024 characters, doesn't that mean 'cin' should get 
into 'eof' state?  And while it's in that state, any reading should 
fail.  Error states can be reset by calling 'cin.clear()' (IIRC) until 
the next read operation which can again set some flags...

Also, see whether the 'std::getline' function is of some use to you.  It 
might be just what you need if you only need to read the input line by 
line...

Good luck!

V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Using cin.get() and cin.eof() ends up in an endless loop Fernando <fernando.arbeiza@gmail.com> - 2011-11-16 07:34 -0800
  Re: Using cin.get() and cin.eof() ends up in an endless loop Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-16 10:38 -0500
    Re: Using cin.get() and cin.eof() ends up in an endless loop Fernando <fernando.arbeiza@gmail.com> - 2011-11-16 08:37 -0800
      Re: Using cin.get() and cin.eof() ends up in an endless loop Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-16 13:11 -0500
        Re: Using cin.get() and cin.eof() ends up in an endless loop Fernando <fernando.arbeiza@gmail.com> - 2011-11-16 11:19 -0800

csiph-web