简体   繁体   English

C ++读取实际输入行

[英]c++ read actual input line

I am trying to get a string line from input. 我正在尝试从输入中获取字符串行。 For example "This is great". 例如“这很棒”。 In all cases I am getting either or "This" either " great". 在所有情况下,我都会得到一个或“ This”或“ Great”。 getline() method did not working as expected. getline()方法未按预期工作。 Code tried: 尝试的代码:

string val;

while (true) {
    cout << message;
    getline(cin, val);
    if (val.length() <= length)
    {
        break;
    }
}

My target is to get all sentence. 我的目标是获得全部判决。 Is that possible? 那可能吗?

adding cin.clear(); cin.sync(); 添加cin.clear(); cin.sync(); cin.clear(); cin.sync(); solve the problem 解决这个问题

string val;

while (true) {
    cin.clear();
    cin.sync();
    cout << message;
    getline(cin, val);
    if (val.length() <= length)
    {
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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