0

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". getline() method did not working as expected. 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?

IntoTheDeep
  • 3,744
  • 13
  • 38
  • 76

1 Answers1

0

adding 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;
    }
}
IntoTheDeep
  • 3,744
  • 13
  • 38
  • 76
  • This is more like sweeping your problem under the rug than solving it, but whatever floats your boat. – n. 1.8e9-where's-my-share m. Jan 11 '17 at 11:43
  • Well, I have describe what is my target. You did not provide any solution. – IntoTheDeep Jan 11 '17 at 11:48
  • Solution to what exactly? Please provide a [mcve] in your question (it is off-topic and will be closed without one). No one can possibly know what your program fragment does out of context. If I guess correctly, you are doing `cin >> something` before the loop, in which case you have a solution mentioned more than once in the comments. Just read them. – n. 1.8e9-where's-my-share m. Jan 11 '17 at 11:52