gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:55:38 25 4
gpt4 key购买 nike

我正在尝试将 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);

}

最佳答案

我确定我们以前经历过这种情况

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)

好多了。

修复该问题(您至少在两个地方有它),如果您的程序仍然无法正常工作,请再次发布。

关于c++ - 尝试使用两个不同的数组来提问和回答问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16268238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com