从未知长度的手动输入中提取单个单词
Pulling individual words from manual input of unknown length
重新发布,因为我在最后一个中没有包含足够的信息。
我尽力了谷歌fu,似乎找不到正确的答案(并不意味着这不是一个愚蠢的错误,因为我还是新手)
int main()
{
vector<string> clauses;
string test;
cout << "Please enter your choice or choicesn ";
while (cin >> test) {
clauses.push_back(test);
}
return 0;
}
我不知道他们会输入多少个选项,所以我把它们扔进一个向量中。
目前,当它运行时,它不会接受任何用户输入,即使按回车键,也只是让该人继续输入。
提前谢谢。保罗
以下代码应该适用于您想要执行的操作:
#include <vector>
#include <string>
#include <iostream>
int main()
{
std::vector<std::string> clauses;
std::string test;
std::cout << "Please enter your choice or choicesn";
do
{
getline(std::cin, test);
if(!test.empty())
clauses.push_back(test);
}while (!test.empty());
return 0;
}
getline 函数将读取键盘上的任何文本并将其推送到您的矢量中,直到用户仅使用 Enter 键,这将结束循环。
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- QTreeView幻灯片多选后无法使用单击选择
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 正则表达式使用提升令牌迭代器在单引号和括号之间提取值
- 从命名空间中提取单个名称
- 从字符串中提取单个字符并将其转换为 int
- 如何从空格分隔的单个字符串中提取多个子字符串
- g++在多个文件上比使用谷歌模拟的单片单个文件慢得多
- Qt:存储 QKeySequence,将其从表单中提取
- 单个多对象卡尔曼滤波器与多个单对象卡尔曼滤波器(多个)
- 从文本文件中提取单个句子...我还没有做对
- 如何在C++中从字节数组(在 BIG-ENDIAN 中)中提取单个字段
- 从未知长度的手动输入中提取单个单词
- 提取单个像素数据的最快方法
- 如何在单击单个按钮后依次转换到不同的qstate
- 如何在单遍历中找到单个链表的中间节点(如果链表的长度未给定)
- Web数据提取和表单填写