Reading an empty C ++ line

I am in a situation where I had a loop, and every time he reads a line, but I do not know how to read empty input. If the user does not enter anything and gets into enter, he remains there.

I want to read this as a string and go to the next input below is the code

int times = 4;
while(times--)
{
    string str;
    cin>>str;
    ---then some other code to play with the string---
}
+3
source share
2 answers

You will need to read the entire line using getline (). Then you will need to tokenize the read lines.

Here is a link to using getline and tokenization using stringstream .

+3
source
char blankline[100];
int times = 4;
while(times--)
{
    //read a blank line
    cin.getline(blankline,100);
    ---then some other code to play with the string---
}
+1
source

Source: https://habr.com/ru/post/1793850/


All Articles