[英]Why does cin cause this program to hang?
我已經在下面發布了相關代碼。 當我編譯程序時,它會運行並到達等待輸入的位置。 我輸入 integer 並按 ENTER,但代碼永遠不會繼續。 我將如何糾正這個問題?
int i;
cout << "Please input column to sort by: ";
cin >> i;
好吧,首先,您在上面發布的內容無法編譯。 試試這個:
#include <iostream>
int main(int argc, char *argv[]) {
int i;
std::cout << "Please input column to sort by: ";
std::cin >> i;
std::cout << "You entered: " << i << "\n";
return 0;
}
使用g++ -O3 thefile.cpp
編譯它,假設文件名為“thefile.cpp”。
如果它不起作用,那么就會出現一個嚴重的問題。 如果是這樣,您應該能夠進一步診斷您的問題。
如果您使用 Visual Studio 2010,請嘗試以下操作:
#include<iostream>
using namespace std;
int main(){
int i;
cout<<"Please input column to sort by: ";
cin>>i;
cout<<"Your input the number: "<<i<<"\n\n";
system("pause");
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.