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

F

Fernando

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

Victor Bazarov

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
 
F

Fernando

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

Victor Bazarov

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
 
F

Fernando

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top