A buddy of mine is writing a little program in MS VC++ that takes input using cin. The problem he is having is that it will read in a file redirected in to it perfectly but when he uses the keyboard for input the program ignores the first eof character. He asked me to take a peek at it and it stumped me too. Now it's bugging me. So I decided to play around with cin a little. I compiled this very short program (see below) to see how it behaved when entering a ^Z from the keyboard. When I compile it with the MS VC++ compiler the binary has the same extact problem as my pal's program. I have to enter two ^Z's on lines by themselves in order the get the program to recognize the eof. Debugging shows that it ignores the first one. I fiddled with cin.ignore and cin.fail and cin.clear. Nothing made a difference.
Here's the strange part. When I compile the exact same source file using MingW (a Win32 port of gcc) it works perfectly! It recognizes the first ^Z and exits exactly as it should.
Does anybody have any idea why the MS comiled program would behave this way but the MingW binary would work perfectly when compiling the exact same source file?
Thanks for the help, Don
Here's the little sample:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int count = 0;
string word;
cin >> word;
while (!cin.eof())
{ count++;
cin >> word;
}
cout << count << " words.\n";
return 0;
}