在if / else语句中调用数组的成员

时间:2015-10-22 03:15:19

标签: c++ arrays if-statement

我正在尝试编写一个if / else语句,说明“如果选择的随机问题是数组中的第一个,请执行此操作....”等对于数组的所有成员。然而,它是默认选择作为阵列的第一个成员并执行该操作的每个问题。我怎样才能把它分开呢?

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?\n", "A)C++\n", "B)DID\n", "C)Intro to Game\n", "D)Yoga" },
                { "Who am I?\n", "A)Bill\n", "B)Nye\n", "C) 24601\n", "D)No one\n" },
                { "Are you actually reading this?\n", "A) Yes\n", "B)No\n", "C)Maybe\n", "D)Who wants to know?\n" },
                { "Will this program work?\n", "A)Of course it will!\n", "B)It might\n", "C)Are you kidding me?\n", "D)Gods only know." },
                { "Where would I rather be?\n", "A)Home\n", "B)Europe\n", "C)Anywhere but here\n", "D)All of the above\n" }
            };
            srand(static_cast<unsigned int>(time(0)));
            int randomQuest = (rand() % NUM_QUEST);
            string question = QUEST[randomQuest][QUESTS];
            cout << question;
            string printAns1 = QUEST[randomQuest][ANS_1];
            string printAns2 = QUEST[randomQuest][ANS_2];
            string printAns3 = QUEST[randomQuest][ANS_3];
            string printAns4 = QUEST[randomQuest][ANS_4];
            cout << printAns1;
            cout << printAns2;
            cout << printAns3;
            cout << printAns4;
            cout << "\nAnswer:";
            string answer;
            cin >> answer;

//问题在这里。 KEEPS默认这个声明并忽略其余因此,它认为答案总是一个

            if (question == QUEST[randomQuest][0])
            {
                if (answer == "A")
                {
                    cout << "Correct. Proceed.";
                }

                else if (answer != "A")
                {
                    cout << "Failure. Leave. Or, you know, try again.";
                }
            }

            else if (question == QUEST[randomQuest][1])
            {
                if (answer == "C")
                {
                    cout << "Correct. Proceed.";
                }

                else if (answer != "C")
                {
                    cout << "Failure. Leave. Or, you know, try again.";
                }
            }

            else if (question == QUEST[randomQuest][2])
            {
                if (answer == "D")
                {
                    cout << "Correct. Proceed.";
                }

                else if (answer != "D")
                {
                    cout << "Failure. Leave. Or, you know, try again.";
                }
            }

            else if (question == QUEST[randomQuest][3])
            {
                if (answer == "C")
                {
                    cout << "Correct. Proceed.";
                }

                else if (answer != "C")
                {
                    cout << "Failure. Leave. Or, you know, try again.";
                }
            }

            else if (question == QUEST[randomQuest][4])
            {
                if (answer == "D")
                {
                    cout << "Correct. Proceed.";
                }

                else if (answer != "D")
                {
                    cout << "Failure. Leave. Or, you know, try again.";
                }
            }


        }

1 个答案:

答案 0 :(得分:0)

你没有发布完整的来源,所以我刚发布了一个如何做到这一点的例子。即使认为它有更多的线,它更易读恕我直言。

#include <stdio.h>
#include <vector>
#include <string>

struct Question
{
    unsigned char answer;
    std::string title;
    std::vector<std::string> options;
};

int main()
{
    std::vector<Question> questions;

    // Define the questions
    Question questionOne;
    Question questionTwo;
    Question questionThree;
    Question questionFour;
    Question questionFive;

    questionOne.answer = 'c';
    questionOne.title = "What course is this?";
    questionOne.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionOne.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionOne.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionOne.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");

    questionTwo.answer = 'a';
    questionTwo.title = "Who am I?";
    questionTwo.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionTwo.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionTwo.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionTwo.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");

    questionThree.answer = 'b';
    questionThree.title = "Are you actually reading this?";
    questionThree.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionThree.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionThree.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionThree.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");

    questionFour.answer = 'c';
    questionFour.title = "Will this program work?";
    questionFour.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionFour.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionFour.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionFour.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");

    questionFive.answer = 'd';
    questionFive.title = "Where would I rather be?";
    questionFive.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionFive.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionFive.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
    questionFive.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");

    // Save them
    questions.push_back(questionOne);
    questions.push_back(questionTwo);
    questions.push_back(questionThree);
    questions.push_back(questionFour);
    questions.push_back(questionFive);

    // Change to a random question accordingly
    for (auto& question : questions)
    {
        // Show question title
        std::cout << question.title << std::endl;

        // Show question options
        for (auto& option : question.options)
        {
            std::cout << option << std::endl;
        }

        // Get the user input accordingly
        unsigned char userInput = 0;

        // Validate user input
        if (userInput == question.answer)
        {
            std::cout << "Congratulations! Next question coming up..." << std::endl;
        }
        else
        {
            std::cout << "Wrong answer. Next question coming up..." << std::endl;
        }
    }

    return 0;
}