C++ 尝试使用两个不同的数组提问和回答问题

C++ 尝试使用两个不同的数组提问和回答问题,c++,arrays,visual-c++,C++,Arrays,Visual C++,我尝试使用2个不同的数组1来回答问题,但当我为任何一个选择的问题选择正确的答案超过“2”时,它总是给我不正确的答案,我不明白为什么有人能抓住我 #include "Questions.h" using namespace std; //struct quiz //{ // string question[MAXITEMS]; // string answers[MAXITEMS]; //}; const int MAXITEMS = 10; int main () { //q

我尝试使用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);

}
#包括“Questions.h”
使用名称空间std;
//结构测验
//{
//字符串问题[MAXITEMS];
//字符串答案[MAXITEMS];
//};
const int max items=10;
int main()
{
//测验列表测验[MAXITEMS];
//
//ifstream问题文件(“Questions2.txt”);
//字符a;
//整数计数=0;
//
//if(!QuestionFile)//文件测试
//  {

//我肯定我们以前也经历过

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)

如果(selectQestion>=0&&selectQestion这没有意义:

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"};
对我来说,答案和问题是不同步的。当然,如果将问题和答案作为结构(或类)的一部分,那么处理这个问题会容易得多:

这段代码并不像你想象的那样:

 if(selectQestion == 0||1||2||3||4||5||6||7||8||9  && tries != 2)
正如另一个答案中所建议的,如果(selectOption==0 | | selectOption==1…
,您可以执行
,但我建议您使用
开关
语句:

 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;
  }
与11项长if条件相比,读取开关要容易得多

另一个问题是:

do {
 ... 
} while(userInput == 1);
变量
userInput
在该循环内没有更改

这里还有一个bug:

 if (userAnswer == answers[0])
您可能需要检查答案是否是所选问题的答案,而不是第一个问题的答案

我可以建议你一次开发代码的较小部分吗?改变一件小事,测试它,如果它不起作用,找出原因,然后再编写一点代码。我甚至没有编译或运行过你的代码,我发现了至少四个明显的错误-是的,我已经做了30多年的编程,所以我有一些发现错误的经验错误。

这是:

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

if (selectQuestion == 1 ...)

符号=右边的表达式是一个表达式,其中C++计算一个值并替换该值代替该长串的“./P>不”,它是<代码>((选择题=0)>1)。

==
的优先级高于
|
。对不起,兄弟,当你这样做的时候,你已经向我展示了它甚至不允许我输入答案,它只是返回来选择一个问题。正如我所说的,如果你的代码仍然不起作用,请再次发布。问题是你在这段代码中有多个错误。简单的错误(就像我在上面修正的那样)首先要修正。然后我们再做更难的。我不明白你的意思,因为前三个问题的第一个答案是有效的,剩下的就行了你的第一个问题和答案不匹配-你的第一个问题是“每件衣服有多少张牌?”,据我所知,答案应该是13-除非最近有一副卡片发生了变化?然后,就像其他人所说的那样,你检查
SelectQestion
的if语句是错误的。我刚刚发现了另一个错误…我将编辑我的答案以覆盖这个错误,因为注释中的代码看起来很糟糕。
if (selectQuestion == 1 ...)