SIGINT处理和getline [英] SIGINT handling and getline
本文介绍了SIGINT处理和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;
}
如果我在程序等待输入时按下ctrl-c,输出:
输入文字:received SIGINT
If I hit ctrl-c while the program waits for input I get the following output:
Enter text: received SIGINT
输入文字:
输入文字:
Enter文本:
输入文字:
Enter text:
Enter text:
Enter text:
Enter text:
(程序继续循环而不等待输入)
(the program continues the loop without waiting for input)
我应该怎么办?
推荐答案
请尝试在 cout
语句:
cin.clear(); // Clear flags
cin.ignore(); // Ignore next input (= Ctr+C)
这篇关于SIGINT处理和getline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文