I modified your code a little bit, and ran it on win98 with VC++6
Code:
#include<iostream>
using namespace std;
int main()
{
int x;
int count = 1;
while (cin>>x)
{
cout<<endl<<endl;
cout<<count++<<"----------"<<endl;
if(cin.eof())
cout<<"eofbit error flag set"<<endl;
if(cin.bad())
cout<<"badbit error flag set"<<endl;
if(cin.fail())
cout<<"failbit or badbit error flag set"<<endl;
}
return 0;
}
Results:
Code:
^Z and no space after 5:
1 2 3 4 5
1----------
2----------
3----------
4----------
5----------
eofbit error flag set
Press any key to continue
Code:
^Z with a space after the 5:
1 2 3 4 5
1----------
2----------
3----------
4----------
5----------
(hangs waiting for more input)
6 (space after 6)
6----------
7(no space after 7)
7----------
eofbit error flag set
Press any key to continue
Code:
^T and no space after the 5:
1 2 3 4 5¶
Ctrl/T just inserts a character in the input stream.
If you have a space after the input, then cin>> will read the last number and terminate it's read when it hits the whitespace, and the operator>> is programmed to leave the whitespace in the stream. The operator>> also skips leading whitespace, so the next time through the loop it can't find any data, so it hangs waiting for input.
If there is no space after the last number, then when cin>> tries to read the last number, it can't finish its read because no whitespace is found, and somehow that causes the eofbit error flag to be set rather than hanging and waiting for more input.