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


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

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 10:38 -0500
Organization A noiseless patient Spider
Message-ID <ja0le0$b4t$1@dont-email.me> (permalink)
References <ab6230fc-c124-4ad3-b7d8-6e7d4847466a@o17g2000yqa.googlegroups.com>

Show all headers | View raw


On 11/16/2011 10:34 AM, Fernando wrote:
> I know this should be a very basic question (I have not any experience
> in C++), but I have been looking for the answer and I still do not
> understand it.
>
> I am trying to use cin.get() for reading from the standard input but
> retaining newline characters, and using cin.eof() to detect the END-OF-
> FILE. But it causes and endless loop. Simplifying the code, this is
> what I am trying to do:
>
> #include<iostream>
>
> using namespace std;
>
> int main () {
>      char buffer[1024];
>
>      while (!cin.eof()) {
>          cin.get(buffer, 1024);
>
>          cout<<  buffer;
>      }
>
>      return 0;
> }
>
> Using cin.good() instead of cin.eof() terminates the loop after the
> first line. It seems to work if I use cin.getline() instead of
> cin.get(), but the newline character are lost.
>
> I am clearly missing something here, but reading the documentation for
> cin.get() did not help me much.
>
> ¿Can somebody give me a hint about what I am missing here?

Have you read the FAQ?  Section 15 seems exactly what you need.

You can find the FAQ here: http://www.parashift.com/c++-faq-lite/

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