PDA

View Full Version : [SOLVED] g++ EOF



cap10Ibraim
March 6th, 2011, 09:17 AM
#include <iostream>
using namespace std;
int main()
{
int character; //use int because char can't represent eof
cout<<"Before input the value of cin.eof() is "<<cin.eof()<<endl
<<"enter a setence "<<endl;
while( (character=cin.get()) != EOF )
cout.put(character);
cout<<"EOF in this system is "<<character<<endl;
return 0;
}



main.cpp: In function ‘int main()’:
main.cpp:8: error: ‘EOF’ was not declared in this scope

why eof is not recognized !

cap10Ibraim
March 6th, 2011, 09:34 AM
i used 0 instead of EOF
but there is an infinite loop when i press ctrl-d ?!

cgroza
March 6th, 2011, 01:11 PM
Maybe you mean cin.eof(), or .eof() on another stream.

ksprasad
March 6th, 2011, 01:25 PM
Hi,

Yes. Use cin.eof() function instead of EOF. there is no such macro defined EOF.

Regards,
ksprasad

cap10Ibraim
March 6th, 2011, 03:51 PM
thanks guys i fixed the while loop as you suggested


while( cin.eof() != 1 ){
character = cin.get();
cout.put(character);
}

cgroza
March 6th, 2011, 06:31 PM
thanks guys i fixed the while loop as you suggested


while( cin.eof() != 1 ){
character = cin.get();
cout.put(character);
}

The while condition could be written as:

while( ! cin.eof() );
Much simpler.

cap10Ibraim
March 6th, 2011, 06:34 PM
The while condition could be written as:

while( ! cin.eof() );
Much simpler.

i know ,but i'm just not used to this style yet