简体   繁体   中英

getting a string c++

I have a problem getting a string. I use

getline(cin,string);

but there is some kind of a bug and it skips a row when I press enter, is there a solution to this problem, or maybe another function to get a string with empty spaces?

My guess is that you are doing cin >> someVar somewhere before you do the getline() .

cin >> someVar
Doesn't read a complete line, but stops on the first whitespace character, and the newline \\n remains unconsumed., which then causes the skipping of line in getline()

If this is the case,

To fix it, you need to add a cin.ignore() statement before the getline() to consume the \\n newline character (or any other extra characters) left in the input stream by the >> stream extractor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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