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