Problem with EOF

Learning c++ and doing an exercise, but cant get it to build.

#include <iostream>
int main()
{
using namespace std;
int ch;
int count = 0;
//Testa för filslut
while ((ch = cin.get()) != EOF) {
cout.put(char(ch));
++count;
}
cout << endl << count << " tecken inlästa.\n";
return 0;
}

when i compile i get this:
$ g++ textin4.cpp -o textin4
textin4.cpp: I funktion "int main()":
textin4.cpp:10:29: fel: "EOF" deklarerades inte i detta definitionsområde
//Rough translation of last line, Error: "EOF" wasn't declared in this definitionarea.
Try this. Make ch a char and change the loop condition to
while (cin.get(ch)) {
Well that did it, why couldn't i use EOF?

Thank you for your help.
I wasn't sure if cin.get() did return EOF on failure but I looked it up and you are right. Your original code should work if you just include a header like <cstdio> that defines EOF.
Topic archived. No new replies allowed.