在c++中调用数组的成员以显示到控制台
Calling members of an array in c++ to display to console
我正试图让我的程序通过一个数组并打印一个随机问题。每个问题都有四个答案,我正试着把它们也打印出来。然而,当我试图让它为它选择的任何随机问题提供具体答案时,它会发出一个错误。什么好主意吗?
if (choice == 1)
{
enum fields{ QUESTS, ANS_1, ANS_2, ANS_3, ANS_4, NUM_FIELDS };
string QUEST[NUM_QUEST][NUM_FIELDS] =
{
{ "What course is this?", "A)C++", "B)DID", "C)Intro to Game", "D)Yoga" },
{ "Who am I?", "A)Bill", "B)Nye", "C) 24601", "D)No one" },
{ "Are you actually reading this?", "A) Yes", "B)No", "C)Maybe", "D)Who wants to know?" },
{ "Will this program work?", "A)Of course it will!", "B)It might", "C)Are you kidding me?", "D)Gods only know." },
{ "Where would I rather be?", "A)Home", "B)Europe", "C)Anywhere but here", "D)All of the above" }
};
srand(static_cast<unsigned int>(time(0)));
int randomQuest = (rand() % NUM_QUEST);
string question = QUEST[randomQuest][QUESTS];
string print = question;
string printAns1 = QUEST[ANS_1];
string printAns2 = QUEST[ANS_2];
string printAns3 = QUEST[ANS_3];
string printAns4 = QUEST[ANS_4];
}
Quest[ANS_1]和它下面的其他是有错误的。它告诉我-
"没有合适的构造函数将"std::string[5]"转换为"std::basic_string, std::allocator>"
QUEST[ANS_1]不是字符串,它是指向数组的指针,如'string[5]'。
尝试:string printAns1 = QUEST[randomQuest][ANS_1];
string printAns1 = QUEST[ANS_1];
string printAns1 = array of strings
行不通,它们是不同的类型。string printAns1[] = array of strings
或string ans1 = QUEST[ANS1][0]; string ans2 = QUEST[ANS1][1];...
相关文章:
- 控制台输出在 Qt5 中未正确显示
- Visual Studio 2017 停止工作,并在打开后显示许多控制台窗口
- 为什么调试时没有显示控制台窗口
- 将控制台输出写入.txt并在控制台上显示
- 当被一个输入分开时,有两个控制台输出同时显示
- 如何在控制台应用程序中添加滚动功能以显示更大的输入
- 在任何 Windows 版本上全屏显示控制台
- C++:显示控制台窗口的输出
- C++:如何在控制台窗口中显示结果
- 控制台输出显示ASCII值,而不是数字的预期数字
- C++/CLI 项目如何在控制台窗口中显示消息
- Linux C LIBEV官方示例显示了冗余控制台行为
- QProcess::startDetached() 不显示控制台窗口
- 关闭应用程序而不显示控制台窗口
- 如何在C++中显示控制台应用程序中的 MFC 对话框
- 不要在发布时显示控制台窗口,而是在调试时显示
- Q使用显示控制台处理STDIN
- 如何在窗口下使用cmake在qt GUI应用程序中显示控制台
- 如何让 MATLAB 的系统() 或 dos() 实时显示控制台输出?
- C++使用 Windows 和 MinGW 在 Eclipse 中编写的程序无法显示控制台视图的输出