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

Discussion in 'C++' started by Fernando, Nov 16, 2011.

  1. Fernando

    Fernando Guest

    Hi,

    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?

    Thanks a lot in advance.
    --
    Fernando Arbeiza
    Fernando, Nov 16, 2011
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Nov 16, 2011
    #2
    1. Advertising

  3. Fernando

    Fernando Guest

    On 16 nov, 16:38, Victor Bazarov <> 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 hate it when I cannot see something that should be so evident...
    --
    Fernando Arbeiza
    Fernando, Nov 16, 2011
    #3
  4. On 11/16/2011 11:37 AM, Fernando wrote:
    > On 16 nov, 16:38, Victor Bazarov<> 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
    Victor Bazarov, Nov 16, 2011
    #4
  5. Fernando

    Fernando Guest

    On 16 nov, 19:11, Victor Bazarov <> wrote:
    > I am uncertain what you were expecting here.


    I was expecting that simple program to copy everything from the
    standard input to the standard output, until EOF. But it only copies
    the first line, be it shorter or longer than 1024 characters.

    > 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...


    But std::getline() gets rid of the newlines. I did want the input as
    it was originally.

    Nevertheless, thanks a lot for your replies. Obviously, there is
    something here that I do not understand. I will continue looking into
    it and I hope sooner or later it will hit me in the head.

    Thanks again.
    --
    Fernando Arbeiza
    Fernando, Nov 16, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandre
    Replies:
    4
    Views:
    496
    Lasse Reichstein Nielsen
    Apr 12, 2005
  2. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,100
    P.J. Plauger
    Oct 17, 2003
  3. Claudio Grondi
    Replies:
    75
    Views:
    1,138
    Tim Peters
    Jan 21, 2006
  4. Replies:
    7
    Views:
    388
    Herbert Rosenau
    Aug 2, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    365
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page