如何在我的C++测验中输入问题
How to Input Questions in My C++ Quiz?
我目前正在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:nn" << "1)Make a quiz?n" << "2) Take a quiz?n" << "3) Quit?nn";
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;
}
}
我不知道这是否是最好的方法?有人可以帮我弄清楚如何使用用户输入进行测验吗?
谢谢!
这是一个很好的方法。据我所知,这是"最佳"方法(如果我错了,有人纠正我(。
但请记住,您不应该使用 cin >> str
,因为它停止在(空格(处。
n
停止在 operator>>
,这是您想要的,因为输入可以包含空格。
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;
};
提出的概念:
- 测验元素是问答对(或类似(。 测验
- 是测验元素的容器。
- 重载流提取CC_6允许测验元素,以从流中读取其自己的成员。 流可以是文本文件。
相关文章:
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 编写时C++中的输入重定向问题
- 输入std::数组时出现问题
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 打印时有二叉树问题.用户输入不打印任何内容
- 用户输入字符串的文件附加问题..C++
- 函数以相反的顺序输出输入问题,并改进算法以解释相等的数字
- 每当调试C (Xcode 10.1)时,输入问题
- iostream输入问题
- 井字输入问题
- 多个玩家的键盘输入问题
- 字符输入问题
- 使用 getline() 的字符串输入问题
- 如何在我的C++测验中输入问题
- GLFW操纵杆输入问题
- 在Bison中为$$输入问题
- c++内存/输入问题
- C++ 拆分输入问题
- c++文件输入问题
- 基本c++输入问题