如何在读取 c++ 输入时脱离循环
How to come out of loop while reading input in c++?
我有一个代码片段,如下所示:
void func() {
string text;
while( std::getline(cin,text)) {
words[i++] = text;
}
}
输入输入时,一旦我完成输入,我就无法退出循环。打印"回车"键无济于事。如何解决这个问题?
将检查替换为:
while( std::getline(cin,text) && text.length())
现在它成功地按下了没有文本的回车键。
相关文章:
- 正在尝试了解输入验证循环
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 通过for循环使用用户输入填充列表
- 计算用户使用循环输入的整数数量
- 从文件循环输入的更合适方法?
- 实现递归函数,避免由 C++ 中 include 的循环调用(没有 #pragma 一次)引起的无限循环输入
- 我应该使用 DeviceContext 函数在管道中循环输入还是通过创建来输入
- 如何在代码中插入循环输入
- 循环输入数据
- 为循环输入并中断C++:至少10次迭代且没有空字符串
- 第一次通过循环输入空字符串?(c++)
- 使用while循环输入的小问题
- 下次不会让我通过 while (true) 循环输入信息
- 我如何让一个数组来存储用户在 C 中使用 do-while 循环输入的最多 10 个元素
- C++循环 - 输入整数,直到用户退出
- 将随机数插入到数组中,其中每个值都是通过 for 循环输入的
- 从文件循环输入流
- 这个函数请求用户通过循环输入文件名的错误在哪里?
- cin.当循环输入时失败
- 如何添加循环输入文件的结果