1

我正在尝试使用 2 个不同的数组 1 作为问题和答案,但是当我为“2”之后的任何选定问题选择正确答案时,它总是给我不正确的答案,我不明白为什么有人可以请我?

#include "Questions.h"

using namespace std;


//struct quiz
//{
//    string question[MAXITEMS];
//  string answers[MAXITEMS];
//};


const int MAXITEMS = 10;

int main ()
{
//quiz listQuiz[MAXITEMS];
//
//ifstream QuestionFile("Questions2.txt");
//char a;
//int count = 0;
//
//  if(!QuestionFile) // file testing
//  {
//    cout<< " error opening file" << endl;
//    return 1;
//  }
//
//  for (int i=0; i<MAXITEMS; i++)
//  {
//      QuestionFile>>listQuiz[i].

  //return 0;

    string question[MAXITEMS] = {"How_many_cards_of_each_suit_are_there?", "How_many_suits_are_there_in_a_standard_pack_of_cards?", "How_many_kings_are_in_a_standard_pack_of_cards?", "How_many_cards_are_in_a_standard_deck_of_cards?","How_many_black_suits_are_there_in_a_standard_pack_of_cards?", "How_many_red_suits_are_in_a_standard_pack_of_cards?", "Whats_the_number_of_the_card_that_comes_before_jack?", "How_many_cards_in_each_set_of_suits_are_there?", "What_is_the_lowest_number_in_a_standard_pack_of_cards?", "What_is_the_highest_number_in_a_standard_pack_of_cards?"};
    string answers[MAXITEMS] = {"4", "4", "4", "52", "2", "2", "10", "13", "2", "10"};

    int userInput = 0;
    int tries = 0;


    bool isGameOver = false;

    cout << "select 1 to start game" << endl;  //gives option to start and quit game
    cout << "select 2 to quit game" << endl;
    cin >> userInput;

    if (userInput == 2)
    {
        isGameOver = true;
        return 0;   
    };
    // error message if 1 or 2 is not input
    do
    {
        if (userInput!=1 && userInput!=2)
        {
            cout << " Your input is not valid! please try again:" << endl; // try switch cases for the different outcomes

            cout << "select 1 to start game" << endl;  
            cout << "select 2 to quit game" << endl;
            cin >> userInput;

            if (userInput == 2)
    {
        isGameOver = true;
        return 0;   
    };
            while (!(cin >> userInput))
            {
                cin.clear(); // clear the error flags
                cin.ignore(INT_MAX, '\n'); // discard the row

                cout << "Your input is not valid! please try again: ";

                cout << "select 1 to start game" << endl;  
                cout << "select 2 to quit game" << endl;
            }
            cout << userInput << endl;


        }
        // reprisent all characters as number to stop while roblem
        // when game starts gives option to select question and shows all questions
        if(userInput == 1)
        {

 //      // system("pause");
    //  //return-1;
 //// };

 // while(QuestionFile)      // while read is working
 // {
    //
    //      // for display

    //QuestionFile >> question[count];    // read into array
    //cout << count << " " << question << endl;
    // count++;

 // }
 // 
 // for (int i = 0; i < count ; ++i)
 // {cout << " array" << i << " is ::";
 // cout << question[i]<< endl;
 // }

 // cout << question[0]; //reads data in cell


 // //QuestionFile.close();

 // //system ("pause");
            do
            {
                cout << "select question" << endl;

                for(int i = 0; i != MAXITEMS; i++)
                {
                    cout << i << " " << question[i] << endl;
                }

                int selectQestion;
                cin >> selectQestion;

                if(selectQestion == 0||1||2||3||4||5||6||7||8||9  && tries != 2)
                {
                    cout << "Enter your answer" << endl;
                    string userAnswer;

                    cin >> userAnswer;

                    while (!(cin >> userAnswer))
                    {
                        cin.clear(); // clear the error flags
                        cin.ignore(INT_MAX, '\n'); // discard the row
                        cout << "Your input is not valid! please try again: ";
                    }

                    if (userAnswer == answers[0])
                    {
                        cout << "Correct answer" << endl;
                    }
                    else{

                        cout << "incorrect try again" << endl;
                        tries++;

                        cin >> userAnswer;
                        if (userAnswer == answers[0])
                        {
                            cout << "Correct answer" << endl;
                        }
                        else 
                            cout << "Incorrect" << endl;

                    }
                }
                if (selectQestion == 0||1||2||3||4||5||6||7||8||9  && tries == 2)
                {
                    cout << "you can no longer answer this question" << endl;
                    cout << "try another question" << endl;
                }

            }
            while(userInput == 1);

        }


    }
    while(isGameOver == false);

}
4

3 回答 3

4

我敢肯定我们以前经历过

if (selectQestion == 0||1||2||3||4||5||6||7||8||9  && tries == 2)

是不正确的。这个版本是正确的。

if ((selectQestion == 0 || selectQestion == 1 || selectQestion == 2 || selectQestion == 3 || selectQestion == 4 || selectQestion == 5 || selectQestion == 6|| selectQestion == 7|| selectQestion == 8 || selectQestion == 9) && tries == 2)

但实际上你应该使用一点逻辑和简化,这个版本更好

if (selectQestion >= 0 && selectQestion <= 9 && tries == 2)

好多了。

修复该问题(您至少在两个地方有它)以及如果您的程序仍然无法再次工作。

于 2013-04-28T21:46:25.867 回答
0

这没有意义:

string question[MAXITEMS] = {"How_many_cards_of_each_suit_are_there?",
"How_many_suits_are_there_in_a_standard_pack_of_cards?",
"How_many_kings_are_in_a_standard_pack_of_cards?", 
"How_many_cards_are_in_a_standard_deck_of_cards?",
"How_many_black_suits_are_there_in_a_standard_pack_of_cards?", 
"How_many_red_suits_are_in_a_standard_pack_of_cards?", 
"Whats_the_number_of_the_card_that_comes_before_jack?", 
"How_many_cards_in_each_set_of_suits_are_there?", 
"What_is_the_lowest_number_in_a_standard_pack_of_cards?", 
"What_is_the_highest_number_in_a_standard_pack_of_cards?"};

string answers[MAXITEMS] = {"4", "4", "4", "52", "2", "2", "10", "13", "2", "10"};

对我来说,答案和问题并不同步。当然,如果您将问答作为结构(或类)的一部分,则处理起来会容易得多:

struct QuestionAndAnswer
{
   std::string question;
   std::string answer;
}

QuestionAndAnswer qAndA[MAXITEMS] = 
{
   { "Question 1", "Answer for question 1" }, 
   { "Question 2", "Answer for question 2" },
   ...
};

而这段代码并没有按照你的想法做:

 if(selectQestion == 0||1||2||3||4||5||6||7||8||9  && tries != 2)

正如另一个答案中所建议的,您可以这样做if (selectOption == 0 || selectOption == 1 ...,但我建议您使用以下switch语句:

 switch(selectOption)
 {
    case 0:
    case 1:
     ... // more cases go here. 
    case 9:
       if (tries != 2)
        ... 
       else // tries == 2
        ... 
       break;
    default:
      ... Stuff to do when input was not a valid selection. 

      break;
  }

阅读 switch 比阅读 11 项长的 if 条件要容易得多。

另一个问题是:

do {
 ... 
} while(userInput == 1);

该变量userInput在该循环内没有改变......

您在这里也有一个错误:

 if (userAnswer == answers[0])

您可能想要检查答案是否是所选问题的答案,而不是第一个问题。

我可以建议您一次开发更小的代码部分 - 更改一点点,测试它,如果它不起作用,找出原因,然后再编写更多代码。我什至没有编译或运行你的代码,而且我发现了至少四个不同的错误——是的,我已经做了 30 多年的编程,所以我有一些发现错误的经验。

于 2013-04-28T21:48:18.060 回答
-1

这个:

if (selectQestion == 0||1||2||3||4||5||6||7||8||9 ...)

相当于:

if (selectQuestion == 1 ...)

== 符号的右侧是一个表达式,C++ 为其计算一个值并将该值替换为一长串 || 的位置。

于 2013-04-28T21:51:30.377 回答