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

Calling members of an array in c++ to display to console

本文关键字:显示 控制台 成员 c++ 调用 数组      更新时间:2023-10-16

我正试图让我的程序通过一个数组并打印一个随机问题。每个问题都有四个答案,我正试着把它们也打印出来。然而,当我试图让它为它选择的任何随机问题提供具体答案时,它会发出一个错误。什么好主意吗?

    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 stringsstring ans1 = QUEST[ANS1][0]; string ans2 = QUEST[ANS1][1];...