In istream, I use cin.getline() to accept some of the characters and I input the EOF signal(as my OS is MAC, I press control + D) then comes the second cin.getline() to accept the rest of the stream. However, I test the value of cin.eof(),3 times, before the first cin.getline(), between the first and second cin.getline() ,and in the end. And in this program, I all use the EOF signal to terminate this three cin.getline();
the code:
#include<iostream>
using namespace std;
int main()
{
cout<<"now EOF:"<<cin.eof()<<endl;
char character[10];
cout<<"input ten digit character,with '!' in the middle,end with EOF(ctrl+d):"<<endl;
cin.getline(character, 10,'!');
cout<<endl;
cout<<"get the character:"<<endl;
cout<<character<<endl;
char character2[10];
cout<<"now EOF:"<<cin.eof()<<endl;
cout<<"press(ctrl+d):"<<endl;
cin.getline(character2, 10,'!');
//cin>>character2;
cout<<endl;
cout<<character2<<endl;
cout<<"now EOF:"<<cin.eof()<<endl;
}
the result is here:
now EOF:0
input ten digit character,with '!' in the middle,end with EOF(ctrl+d):
123!456
get the character:
123
now EOF:0
press(ctrl+d):
456
now EOF:1
but when I substitute cin.getline(character2, 10,'!') with the commented part cin>>character2 :
the result is:
now EOF:0
input ten digit character,with '!' in the middle,end with EOF(ctrl+d):
123!456
get the character:
123
now EOF:0
press(ctrl+d):
456
now EOF:0
I want to know,why this happen and how the cin.eof() value change. Thank you!