The cin.get() member function with no arguments returns the next character from the input. That is you use it in this way:
ch = cin.get();
Recall that cin.get(ch) returns an object, not the character read!
Important, to test on the EOF when reading characters, you have to make ch to be an int, since the EOF is most of the times -1. a char could not represent that value.
EOF is a constant, defined in the iostream header file.
The EOF must be different from any other valid character.
Just use the EOF in a program:
int ch; // for compatibility with EOF value
ch = cin.get()
while (ch != EOF)
{
cout.put(ch); // cout.put(char(ch)) for some implementations
++count;
ch = cin.get();
}
If ch is a character, the loop displays it. If ch is EOF, the loop terminates.
You should realize that EOF does not represents a character in the input. Instead it's a signal that there are no more characters.
Abonneren op:
Reacties posten (Atom)
Geen opmerkingen:
Een reactie posten