I'm working on a quiz project in C++ at the moment, and I'm confused as to how to make some of it work. I have a class (quiz) which sets up the rules for how the quiz works. It looks like this:
Code:
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(intQuestion_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;
}
elseif(pom <(num_q /2))
{
cout << minus << endl;
cout <<"your score was "<< pom << endl;
}
else
{
cout <<"input not recognised"<< endl;
}
system("pause");
}
};
To run a quiz using this, you use a format like this in Main():
Code:
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');
The above works absolutely fine when I run it using Politics.run_quiz().
The issue is that I want to create a menu that allows me to make or take the quizzes, and I am confused as to how I code the maker part. In the end I want to save this information to a file, and when the user selects 'take' in the menu, they will load this file and the quiz will be run from it. So far, I have this just as an example to illustrate what I want:
Code:
int main()
{
intChoice;
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;
intNum_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){
return0;
}
}
I have no idea whether this is the best way to go about it or not? Could someone please help me work out how to make a quiz using user inputs?
Thanks!