如何使用c++;? 我试图用C++随意显示一个用户输入字符串,但是我找不到一个方法。 目前我正在预先定义一些字符串 #include<iostream> #include<string> #include<cs

如何使用c++;? 我试图用C++随意显示一个用户输入字符串,但是我找不到一个方法。 目前我正在预先定义一些字符串 #include<iostream> #include<string> #include<cs,c++,codeblocks,dev-c++,C++,Codeblocks,Dev C++,如何使用c++;? 我试图用C++随意显示一个用户输入字符串,但是我找不到一个方法。 目前我正在预先定义一些字符串 #include<iostream> #include<string> #include<cstdlib> #include<ctime> using namespace std; int main() { srand(time(0)); const string wordlist[4] = {"hi","h

如何使用c++;? 我试图用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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{ 
srand(时间(0));
const string单词列表[4]={“嗨”,“你好”,“最近好”,“wassup”};
string word=wordlist[rand()%4];

要做到这一点,您必须首先从
wordlist
数组中删除
const
限定符

srand(time(0));
std::vector<string> wordlist(4);
for(auto& s: wordlist) std::cin>>s;
string word = wordlist [rand()%4];
cout<<word;
srand(时间(0));
std::向量词表(4);
对于(自动&s:wordlist)std::cin>>s;
string word=wordlist[rand()%4];

cout
stand(time(0));
一些使用
std::cin
的例子应该就在你最喜欢的
std::cout
例子的旁边,你到底在做什么?你似乎知道如何在数组中随机选取索引,这是关于获取用户输入的问题吗?@JoyDey“是的,我正在努力处理用户输入。"您可能需要一个
std::vector
来存储用户输入的单词。如果字符串数始终为4,
std::array
可能比
std::vector
看起来不是从用户输入中读取
wordlist
的正确方法要好一些。出现错误。基于范围的“for”循环是在C++98模式下不允许这样做这不是一个好方法,对于一个新手来说也不是一个好答案ῥεῖ 嗯……看起来会有用(至少在某种程度上)?@M.K有什么我需要进一步澄清的吗?