gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:33:11 25 4
gpt4 key购买 nike

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

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

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

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

while (cin >> test) {
clauses.push_back(test);
}

return 0;
}

我不知道他们会输入多少个选项,所以我将它们放在一个 vector 中。

目前,当它运行时,它不会接受任何用户输入,只是让用户在按下回车键时继续输入。

提前致谢。保罗

最佳答案

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

#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;
}

getline 函数将读取键盘上的任何文本并将其推送到您的 vector 中,直到用户仅使用 Enter 键,这将结束循环。

关于c++ - 从未知长度的手动输入中提取单个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34865282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com