c++ - 在 C++ 中调用数组成员以显示到控制台

标签 c++ arrays random

我试图让我的程序遍历一个数组并打印一个随机问题。每个问题都有四个答案,我也试图让它打印出来。然而,当我试图让它为它选择的任何随机问题提取具体答案时,它会发出错误。有什么想法吗?

    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];

关于c++ - 在 C++ 中调用数组成员以显示到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33272077/

相关文章:

javascript - 计算数组中的对象数量?

objective-c - 用随机字节填充内存 - C/Objective-C

.net - .Net GUID 的确定性如何?

来自查询字符串的 javascript/jquery 数组

c# - 在 'circular' 数组中寻找下一个位置

mysql - 如何优化 MySQL 的 ORDER BY RAND() 函数?

c++ - 欧氏距离使用内在指令

c++ - 如果两个数字是相同的 0,则在 C++ 和 Root 中创建 0 条件

c++ - 编写一个概念检查库,但在所有基本算术类型上都失​​败了

c++ - 以编程方式清除 Visual Studio 输出窗口