Question

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.

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top