Read blank line C++

筅森魡賤 提交于 2019-12-10 21:16:22

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/5077999/read-blank-line-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!