如何使用getline()避免空白输入
how to avoid blank input by using getline()?
当我只按回车键而没有输入任何内容时,getline()
函数也会收到空白输入。如何修复不允许空白输入(有字符和/或数字和/或符号)?
string Keyboard::getInput() const
{
string input;
getline(cin, input);
return input;
}
只要输入为空,就可以继续重新执行getline。例如:
string Keyboard::getInput() const
{
string input;
do {
getline(cin, input); //First, gets a line and stores in input
} while(input == "") //Checks if input is empty. If so, loop is repeated. if not, exits from the loop
return input;
}
试试这个:
while(getline(cin, input))
{
if (input == "")
continue;
}
string Keyboard::getInput() const
{
string input;
while (getline(cin, input))
{
if (input.empty())
{
cout << "Empty line." << endl;
}
else
{
/* Some Stuffs */
}
}
}
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 无法通过空白将文本文件行分隔为矢量
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 检测整数类型变量上的空白输入
- 输入层类型:图像数据在窗口咖啡cpp中给出空白输出
- 为什么从文件输入中读取 std::string 会返回空白
- C++,如何获得多个输入除以空白
- 如何使用getline()避免空白输入
- 如何使用空白作为分隔符对字符串进行标记并将标记输入到列表中