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
  • 4,027
  • 15
  • 39
  • 84
  • 2
    `getline` and `cin >>` don't mix well, because they handle newline characters differently. There are quite a few SO questions about it, I'm not really sure which to recommend, but for instance [here](http://stackoverflow.com/q/6649852/1171191) and [here](http://stackoverflow.com/q/5739937/1171191). – BoBTFish Jan 11 '17 at 09:00
  • Thanks, but solutions in links did not working. It is funny though that I am wasting hours for something so basic, that not working as it should be. No solution by far... – IntoTheDeep Jan 11 '17 at 09:16
  • First you read a line with `getline(cin, val);`, then you overwrite `val` with a single word using `cin >> val`. – molbdnilo Jan 11 '17 at 09:27
  • Is that suggestion, or? – IntoTheDeep Jan 11 '17 at 09:32
  • No, that's what your code is doing. – molbdnilo Jan 11 '17 at 09:34
  • Ok, thanks for note. – IntoTheDeep Jan 11 '17 at 09:36
  • If you want to slurp an entire line into a single string, including consuming (and discarding) the trailing newline, use [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline). If you want to slurp a single word (contiguous sequence of non-whitespace characters) after skipping leading whitespace characters and stopping at the first encounter of any whitespace or end-of-stream-input, use [`operator >>`](http://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt). Pick *one* based on what your goal is. – WhozCraig Jan 11 '17 at 09:42
  • It looks like you have edited your code without editing the rest of the question. Does the edited code still exhibits the **same** problem? – n. m. could be an AI Jan 11 '17 at 10:08
  • yes, this is same 100% – IntoTheDeep Jan 11 '17 at 10:09
  • So which one is that? Are you getting "this", "is", "great" or something else? – n. m. could be an AI Jan 11 '17 at 10:10
  • First loop return empty and it goes to second loop – IntoTheDeep Jan 11 '17 at 10:15
  • If I add cin.ignore() it works, but it deletes first letter from 2nd loop till end – IntoTheDeep Jan 11 '17 at 10:18
  • for example I add three times "This is great". current code skips first one and goes to second loop. If I add cin.ignore() first tome works just fine, but second and third output "his is great" – IntoTheDeep Jan 11 '17 at 10:20
  • 1
    please provide [complete](http://stackoverflow.com/help/mcve) example so we can reproduce – apple apple Jan 11 '17 at 10:32
  • You need not call `cin.ignore` in a loop. Only after `cin>>` before `getline`. – n. m. could be an AI Jan 11 '17 at 10:34
  • @TeodorKolev if you want to answer someone in particular, use the @ convention. – n. m. could be an AI Jan 11 '17 at 10:36

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
  • 4,027
  • 15
  • 39
  • 84
  • This is more like sweeping your problem under the rug than solving it, but whatever floats your boat. – n. m. could be an AI 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. m. could be an AI Jan 11 '17 at 11:52