读取空白行C ++ [英] Read blank line C++

查看:96
本文介绍了读取空白行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.

这篇关于读取空白行C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆