我正在尝试让我的程序经过一个数组并打印一个随机问题。每个问题都有四个答案,我也试图将其打印出来。但是,当我尝试让它为选择的任何随机问题提取特定答案时,都会出错。有任何想法吗?

    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/

10-12 16:18