简体   繁体   中英

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 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM