开发者

Read blank line C++

开发者 https://www.devze.com 2023-02-12 20:19 出处:网络
I am in a situation where i had a loop and everytime it reads a string but I dont know how to read blank input i.e if user enter nothing and hit enter, it remains there.

I am in a situation where i had a loop and everytime it reads a string but I dont know how to read blank input i.e if user enter nothing and hit enter, it remains there.

I want to read that as string and m开发者_C百科ove to next input below is the code

int times = 4;
while(times--)
{
    string str;
    cin>>str;
    ---then some other code to play with the string---
}


You would need to read the entire line using getline(). Then you would need to tokenize the strings read.

Here is a reference on using getline and tokenizing using stringstream.


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---
}
0

精彩评论

暂无评论...
验证码 换一张
取 消