문제

How can I signal XTerm terminal to end of input. In my case, I run a C++ program in XTerm console and I want to signal the program the end of input by pressing some combination of keyboard buttons.(I tried Ctrl+D Ctrl+Z ).My program goes like this :

map<int,string>info;
string name;
int age;
cin>>name;
while( **?????????** ){   //Input till EOF , missing logic
    cin>>age;
    info.insert( pair<int,string>(age,name) );
    cin>>name;
}

The program proceeds upon receiving the end of input signal from terminal.

도움이 되었습니까?

해결책

You always need to check the input after reading, i.e., your program should look something like this:

while (std::cin >> name >> age) {
    // do something with name and age
}

This will read from std::cin until something fails. You can check if std::cin.eof() is set to determine if having reached the end of the of the input is the cause of the error or there was some other failure, e.g., an attempt to enter something which isn't a number for the age.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top