mercredi 21 octobre 2015

Calling a member of an array in an if/else statement

I'm trying to write an if/else statement that says 'if the the random question chosen is the first in the array do this ....' etc for all the members of the array. However it is defaulting every question chosen as the first member of the array and carrying out that action. How can I get it to separate that?

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;

//PROBLEM IS HERE. KEEPS DEFAULTING TO THIS IF STATEMENT AND IGNORING THE REST SO THAT IT THINKS THE ANSWER IS ALWAYS A

            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.";
                }
            }


        }

Aucun commentaire:

Enregistrer un commentaire