Why does cin cause this program to hang?
I have posted the relevant code below. When I compile the program, it runs and reaches the point where it waits for the input. I type in an integer and press ENTER, but the code never continues. How开发者_C百科 would I go about correcting this?
int i;
cout << "Please input column to sort by: ";
cin >> i;
Well, first of all, what you posted above won't compile. Try this instead:
#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;
}
Compile it with g++ -O3 thefile.cpp
, assuming the file is called "thefile.cpp".
If it doesn't work then there is a serious issue going on. If it does you should be able to diagnose your issue further.
If you use visual studio 2010 try this:
#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;
}
精彩评论