【发布时间】:2019-08-05 19:22:38
【问题描述】:
我正在尝试使用 c++ 随机显示用户输入字符串,但我找不到方法。 目前我正在预定义一些字符串
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(0));
const string wordlist[4] = {"hi","hello","what's up","wassup"};
string word = wordlist [rand()%4];
cout<<word;
return 0;
}
我想要的是:-我不想预先定义它们。我希望用户输入 4 个单词,然后我将显示用户给出的 4 个单词中的一个单词(随机)。
【问题讨论】:
-
stand(time(0));?使用std::cin的一些示例应该在您最喜欢的c++ book 中使用std::cout的示例旁边 -
你到底在纠结什么?您似乎知道如何在数组中随机选择一个索引,是关于获取用户输入的问题吗?
-
@JoyDey “是的。我正在为用户输入而苦苦挣扎。”您可能想要一个
std::vector<std::string>来存储用户输入的单词。 -
如果字符串的数量始终为 4,
std::array可能比std::vector稍微增加一点
标签: c++ codeblocks dev-c++