Using `getline(cin, s);` after using `cin >> n;`

前端 未结 3 1403
广开言路
广开言路 2021-01-07 02:02
int n;
std::cin >> n;

std::string s = \"\";
std::getline(cin, s);

I noticed that if I use cin, my program would hang the next t

3条回答
  •  甜味超标
    2021-01-07 02:25

    int n;
    std::cin >> n;
    
    
    std::cin.get() //<--- use cin.get() here ...
    
    
    std::string s = "";
    std::getline(cin, s);
    

提交回复
热议问题