Getline keeps on getting newline character. How can I avoid this?

前端 未结 6 1709
离开以前
离开以前 2020-12-14 23:56

Basically I first takes an integer as input and then test case follows. My each test case is an string. I am suppose to print the string back if the starting patten of strin

6条回答
  •  旧时难觅i
    2020-12-15 00:13

    So, your real problem isn't that getline eats newlines, but that your second getline(cin, ne) is eating a line...

    And that is because you mistakenly think that you need two getline operations to read one line - or something like that. Mixing "linebased" and "itembased" input does have confusing ways to deal with newlines, so you do need something to "skip" the newline left behind frin cin >> N;, but once you have got rid of that, you only need ONE getline to read up and including the newline at the end of a line.

提交回复
热议问题