我目前正在用C ++开展一个测验项目,我对如何使其中的一些工作感到困惑。我有一个课程(测验),它设置了测验如何工作的规则。它看起来像这样:
class quiz
{
private:
//how many questions
int num_q;
char answer[50];
string question[50];
int pom;
string plus;
string minus;
public:
quiz()
{
pom = 0;
}
void set_pom_answers(string pluss, string minuss)
{
plus = pluss;
minus = minuss;
}
void set_num_q(int num_qq)
{
num_q = num_qq;
}
//Question_Number = question number, ca = correct answer, and q = question
void set_q_a(int Question_Number, string q, char ca)
{
question[Question_Number - 1] = q;
answer[Question_Number - 1] = ca;
}
void run_quiz()
{
char ans;
for (int i = 0; i < num_q; i++)
{
cout << question[i] << ": ";
cin >> ans;
if (ans == answer[i])
{
cout << "Correct! good for you!" << endl;
pom = pom + 1;
}
else
{
cout << "Wrong. :(" << endl;
pom = pom;
}
system("pause");
system("cls");
}
if (pom >= (num_q / 2))
{
cout << plus << endl;
cout << "your score was " << pom << endl;
}
else if (pom < (num_q / 2))
{
cout << minus << endl;
cout << "your score was " << pom << endl;
}
else
{
cout << "input not recognised" << endl;
}
system("pause");
}
};
要使用此操作运行测验,请在Main()中使用这样的格式:
quiz Politics;
Politics.set_num_q(10);
Politics.set_pom_answers("You're good at Politics.", "Revise more!");
Politics.set_q_a(1, "Michael Fallon is the current foreign secretary: [t]rue or [f]alse", 'f');
Politics.set_q_a(2, "Tony Blair was PM from 1996-2007: [t]rue or [f]alse", 'f');
Politics.set_q_a(3, "Michael Gove is the current education secretary : [t]rue or [f]alse", 'f');
Politics.set_q_a(4, "Teresa May is the current home secretary: [t]rue or [f]alse", 't');
Politics.set_q_a(5, "Owen Jones is a blairite: [t]rue or [f]alse", 'f');
Politics.set_q_a(6, "Nick Robinson is the BBC's political editor: [t]rue or [f]alse", 'f');
Politics.set_q_a(7, "David Cameron is a eurosceptic: [t]rue or [f]alse", 't');
Politics.set_q_a(8, "Matthew Handcock is a Tory MP: [t]rue or [f]alse", 't');
Politics.set_q_a(9, "Jonathon Woodcock is a Labour MP: [t]rue or [f]alse", 't');
Politics.set_q_a(10, "David McDonell is the current shadow chancellor: [t]rue or [f]alse", 'f');
当我使用Politics.run_quiz()运行它时,上述工作绝对正常。
问题在于我想创建一个菜单,允许我制作或参加测验,而我对如何编码制作者部分感到困惑。最后,我想将此信息保存到文件中,当用户在菜单中选择“take”时,他们将加载此文件并从中运行测验。到目前为止,我只是作为一个例子来说明我想要的东西:
int main()
{
int Choice;
do {
cout << "Would you like to:\n\n" << "1)Make a quiz?\n" << "2) Take a quiz?\n" << "3) Quit?\n\n";
cin >> Choice;
} while (Choice > 3);
if (Choice == 1) {
string Quiz_Name;
cout << "What is the name of this quiz?\n";
cin >> Quiz_Name;
int Num_of_Qs;
cout << "How many Questions should there be?\n";
cin >> Num_of_Qs;
string Answer_Plus;
cout << "What message should be displayed if the score is above 50%?\n";
cin >> Answer_Plus;
string Answer_Minus;
cout << "What message should be displayed if the score is less that 50%?\n";
cin >> Answer_Minus;
/*Here I would like to add all the elements together, along with the questions and answers. for example:
quiz Quiz_Name;
Quiz_Name.set_num_q(Num_of_Qs);
Quiz_Name.set_pom_answers(Answer_Plus, Answer_Minus);
for (int j = 1; j <= Num_of_Qs; j++) {
Quiz_Name.set_q_a(j, [The user inputs a question], [the user inputs an answer])
}
or something along those lines? How would I make something like this work?
*/
}
if (Choice == 2) {
Politics.run_quiz();
}
if (Choice == 3) {
return 0;
}
}
我不知道这是否是最佳方式?有人可以帮我解决如何使用用户输入进行测验吗?
谢谢!
答案 0 :(得分:0)
这是一个非常好的方法。据我所知,这是“最好”的做法(如果我错了,有人会纠正我)。
请记住,您不应该使用cin >> str
,因为它会在(空格)处停止。
getline(cin, str)
停在\n
,这是您想要的,因为输入可以包含空格。
答案 1 :(得分:0)
TL; DR。
我建议使用具有问题和答案的结构:
class Question_Answer
{
std::string question;
std::string answer;
};
你的测验将是容器的问题和答案:
typedef std::vector<Question_Answer> Quiz_Container;
Quiz_Container politics_quiz;
您可以重载流提取operator>>
以使对象从流(即文件)中读取其成员:
class Question_Answer
{
std::string question;
std::string answer;
friend std::istream& operator>>(std::istream& input,
Question_Answer& qa);
};
std::istream& operator>>(std::istream& input,
Question_Answer& qa)
{
std::getline(input, question);
std::getline(input, answer);
return input;
}
输入循环可以实现为:
Question_Answer quiz_element;
Quiz politics_quiz;
while (politic_questions_file >> quiz_element)
{
politics_quiz.push_back(quiz_element);
}
这可以扩展,以便问题可以有选择和答案选择:
class Question_Answer
{
std::string question;
std::vector<std::string> choices;
unsigned int answer_choice;
};
提出的概念:
operator>>
允许测验
元素从流中读取自己的成员。流可以是一个
文本文件。