读取空白行C ++
问题描述:
我处于一种循环状态,每次它读取一个字符串,但我不知道如何读取空白输入,即如果用户什么都不输入然后按Enter,它将保留在那里。
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---
}
答
您需要使用getline()阅读整行。然后,您需要标记读取的字符串。
You would need to read the entire line using getline(). Then you would need to tokenize the strings read.
此处是使用 getline 并使用 stringstream a>。
Here is a reference on using getline and tokenizing using stringstream.