C++ 从未知长度的手动输入中提取单个单词

C++ 从未知长度的手动输入中提取单个单词,c++,vector,user-input,C++,Vector,User Input,重新发布,因为我在上一篇文章中没有包含足够的信息 我尽了最大努力在谷歌上搜索,但似乎找不到正确的答案(这并不意味着这不是一个愚蠢的错误,因为我还是新手) intmain() { 向量从句; 串试验; cout>测试){ 条款。推回(测试); } 返回0; } 我不知道他们会输入多少个选项,所以我把它们放在一个向量中 当前,当它运行时,它不会接受任何用户输入,只允许用户在按enter键时继续键入。c 提前谢谢。Paul以下代码适用于您想要执行的操作: #include <vector>

重新发布,因为我在上一篇文章中没有包含足够的信息

我尽了最大努力在谷歌上搜索,但似乎找不到正确的答案(这并不意味着这不是一个愚蠢的错误,因为我还是新手)

intmain()
{
向量从句;
串试验;
cout>测试){
条款。推回(测试);
}
返回0;
}
我不知道他们会输入多少个选项,所以我把它们放在一个向量中

当前,当它运行时,它不会接受任何用户输入,只允许用户在按enter键时继续键入。c


提前谢谢。Paul

以下代码适用于您想要执行的操作:

#include <vector>
#include <string>
#include <iostream>

int main()
{
    std::vector<std::string> clauses;
    std::string test;

    std::cout << "Please enter your choice or choices\n";

    do
    {
        getline(std::cin, test);
        if(!test.empty())
            clauses.push_back(test);

    }while (!test.empty());

    return 0;
}
#包括
#包括
#包括
int main()
{
向量子句;
字符串测试;

std::cout可能与格式化提取有关,例如
运算符>>
忽略和跳过空白。使用
std::getline
。可能的重复问题可以归结为:“如何读取行?”非常感谢Isuka。对于任何后来通过谷歌找到它的人来说,Isuka发布的代码非常有效。
#include <vector>
#include <string>
#include <iostream>

int main()
{
    std::vector<std::string> clauses;
    std::string test;

    std::cout << "Please enter your choice or choices\n";

    do
    {
        getline(std::cin, test);
        if(!test.empty())
            clauses.push_back(test);

    }while (!test.empty());

    return 0;
}