尝试使用两个不同的数组来提问和回答问题
Trying to use two diffrent arrays to ask and answer questions
我试图使用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)
好多了。
修复这个问题(你至少有两个地方有这个问题),如果你的程序仍然不能工作,
这没有意义:
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;
}
读开关比读11项长的if-condition要容易得多。
另一个问题是:
do {
...
} while(userInput == 1);
变量userInput
在该循环中不改变…
这里也有一个bug:
if (userAnswer == answers[0])
您可能想要检查答案是所选问题的答案,而不是第一个问题的答案。
我建议你一次只开发一小部分代码——改变一小部分,测试它,如果它不起作用,找出原因,然后写更多的代码。我甚至没有编译或运行过您的代码,但我已经发现了至少四个明显的错误——是的,我已经做了30多年的编程,所以我在发现错误方面有一些经验。
This:
if (selectQestion == 0||1||2||3||4||5||6||7||8||9 ...)
等价于:
if (selectQuestion == 1 ...)
==符号的右侧是一个表达式,c++为其计算一个值,并将该值替换为长序列的||。
相关文章:
- 数组C++问题中最大和第二大的数字
- 二维数组问题
- 数组问题:变量周围的堆栈'arr'已损坏
- 无符号字符数组问题
- 数组问题(if (zeds[i].x == zeds[i].x))
- 字符数组问题
- C++中的turbo数组问题
- 输入和输出的2D字符数组问题
- 多维数组问题
- 指针和数组指向数组问题
- 如何随机排列数组问题
- C++ 多维数组问题 - 多次打印条目
- C++ 2D 读入数组问题
- C++读入 2D 数组问题
- 指针和多维数组问题
- C++多项式类,指针数组问题
- 在数组问题(C++)中查找偶数
- 二维std::数组问题
- 向量/数组问题,不能给变量赋索引/不能使用变量作为数组大小(索引)c++
- 指针数组问题