SIGINT processing and getline… here is a solution to the problem.
SIGINT processing and getline
I wrote this simple program :
void sig_ha(int signum)
{
cout<<"received SIGINT\n";
}
int main()
{
string name;
struct sigaction newact, old;
newact.sa_handler = sig_ha;
sigemptyset(&newact.sa_mask);
newact.sa_flags = 0;
sigaction(SIGINT,&newact,&old);
for (int i=0; i<5; i++)
{
cout<<"Enter text: ";
getline(cin,name);
if (name!="")
cout<<"Text entered: "<<name;
cout<<endl;
}
return 0;
}
If I press Ctrl+C while the program is waiting for input, I get the following output:
Enter text: SIGINT received
Enter text:
Enter text:
Enter text:
Enter text:
(The program continues to loop and does not wait for input).
What am I going to do?
Solution
Try adding the following before your cout
statement:
cin.clear(); Clear flags
cin.ignore(); Ignore next input (= Ctr+C)