c-cin.get()是非阻塞的
作者:互联网
我有链接问题中提到的相同问题.运行该程序后,控制台窗口(在VS 2010中)立即消失.我使用cin.get();在主要功能的末尾,但问题仍然存在.对可能的原因有任何想法吗?您可以在main中检出代码:
int main()
{
const int arraysize = 10;
int order;
int counter;
int a[arraysize] = {2,6,4,45,32,12,7,33,23,98};
cout<<"Enter 1 to sort in ascending order\n"
<<"Enter 2 to sort in descending order\n";
cin>>order;
cout<<"Data items in original order\n";
for(counter=0;counter<arraysize;counter++){
cout<<setw(4)<<a[counter];
}
switch (order){
case 1: cout<<"\nData items in ascending order\n";
selectionSort(a, arraysize, ascending);
break;
case 2: cout<<"\nData items in descending order\n";
selectionSort(a, arraysize, descending);
break;
default: return 0;
}
for(counter=0;counter<arraysize;counter++){
cout<<setw(4)<<a[counter];
}
cout<<endl;
cin.get();
return 0;
}
链接:C++ on Windows – the console window just flashes and disappears. What’s going on?
解决方法:
我的猜测是
default: return 0;
被执行.
编辑:
没错,这不是问题.读取this.
快速解决方法是:
cout<<endl;
cin.ignore(); // <---- ignore previous input in the buffer
cin.get();
但是您可能想阅读该文章以获取有关该行为的更多信息.
标签:c,visual-c,visual-studio-2010,console-application 来源: https://codeday.me/bug/20191012/1901310.html