The C++ program is a quiz application that asks the user 10 multiple choice questions to test their knowledge of synonyms and antonyms. It initializes global variables to track the user's guess and total score. A Question class is used to define the attributes for each question like text, answers, correct answer, and score. Main creates Question objects for each question and calls methods to set the question attributes and display the questions. After displaying all questions, it calculates the total score and prints a passing or failing message based on getting at least 70% correct.
1. #include <iostream>
#include <string>
using namespace std;
int Guess;
int Total;
class Question
{
private:
string Question_Text;
string Answer_1;
string Answer_2;
string Answer_3;
string Answer_4;
int Correct_Answer;
int Question_Score;
public:
void setValues (string,string,string,string,string, int, int);
void askQuestion();
};
int main()
{
cout<<"Welcome to the Quiz Master"<<endl;
cout<<"Synonym & Antonym"<<endl;
cout<<"INSRUCTION"<<endl;
cout<<"*********************************************
**********"<<endl;
cout<<"* Choose one of the possible out of four. *"<<endl;
cout<<"* You have to get at least 70 or above *"<<endl;
cout<<"* to pass the quiz. *"<<endl;
2. cout<<"*********************************************
**********"<<endl;
cout<<"Press ENTER to start the quiz"<<endl;
cin.get();
string Name;
cout<<"What is your name?"<<endl;
cin>> Name;
string Respond;
cout<<"Are you ready to take the quiz, "<<Name<<"?
Yes/No?"<<endl;
cin>>Respond;
if (Respond == "yes")
{
cout<<endl;
cout<<"Good Luck!"<<endl;
cout<<endl;
}
else if (Respond == "no")
{
cout<<"Goodbye!"<<endl;
return 0;
}
Question q1;
Question q2;
Question q3;
Question q4;
Question q5;
Question q6;
Question q7;
Question q8;
Question q9;
Question q10;
q1.setValues("what is the synonym of stupid?",
"dumb",
"smart",
3. "clever",
"genious",
1,
10);
q2.setValues("what is the synonym of beautiful? ",
"helpful",
"happy",
"ugly",
"pretty",
4,
10);
q3.setValues("what is the synonym of stretch?",
"fetch",
"intense",
"expand",
"tease",
3,
10);
q4.setValues("what is the synonym of happy?",
"negative",
"impossible",
"joyful",
"hateful",
3,
10);
q5.setValues("what is the synonym of good?",
"dream",
"excellent",
"recycle",
"mean",
2,
10);
q6.setValues("what is the antonym of dark?",
"bright",
"green",
"dim",
4. "cloudy",
1,
10);
q7.setValues("what is the antonym of sucessful? ",
"stright",
"pass",
"fullfil",
"fail",
4,
10);
q8.setValues("what is the antonym of tight?",
"tiny",
"small",
"loose",
"stress",
3,
10);
q9.setValues("what is the antonym of advantage? ",
"benefit",
"disadvantage",
"favor",
"improvement",
2,
10);
q10.setValues("what is the antonym of strong?",
"hard",
"soft",
"weak",
"power",
3,
10);
q1.askQuestion();
q2.askQuestion();
q3.askQuestion();
q4.askQuestion();
5. q5.askQuestion();
q6.askQuestion();
q7.askQuestion();
q8.askQuestion();
q9.askQuestion();
q10.askQuestion();
cout<<"Your total score is"<<Total<<"out of 100"<<endl;
cout<<endl;
if(Total >= 70)
{
cout<<"Great, you passed the quiz!"<<endl;
cout<<endl;
cout<<"Congratulation"<<endl;}
else
{
cout<<"Sorry, you failed the quiz"<<endl;
cout<<"Better luck next time!"<<endl;
}
return 0;
}
void Question::setValues(string q, string a1, string a2, string
a3, string a4, int ca, int qs)
{
Question_Text = q;
Answer_1 = a1;
Answer_2 = a2;
Answer_3 = a3;
Answer_4 = a4;
Correct_Answer = ca;
Question_Score = qs;
}
6. void Question:: askQuestion()
{
cout<<endl;
cout<< Question_Text <<endl;
cout<<"1."<<Answer_1<<endl;
cout<<"2."<<Answer_2<<endl;
cout<<"3."<<Answer_3<<endl;
cout<<"4."<<Answer_4<<endl;
cout<<endl;
cout<<"what is your answer?"<<endl;
cin>>Guess;
if (Guess == Correct_Answer)
{
cout<<endl;
cout<<"Correct!"<<endl;
Total = Total + Question_Score;
}
else
{
cout<<endl;
cout<<"Incorect!"<<endl;
cout<<"The correct answer is "<<
Correct_Answer<<"."<<endl;
}
}
Please give me some comments as well as the clues of how this
program works. Thank you
7. Solution
Program Execution starts from the main method.The program
will wait till the user presses enter button.And asks for the
name, and asks for whether he is ready to take the quiz.if the
user enters “yes” It will start the quiz by wishing you good
luck.If the user enters “no” the program will terminate by
displaying a message good bye.
If the user enters “yes” then the program will create multiple
instances to the class “Question”.
Question q1;
Question q2;
Question q3;
Question q4;
Question q5;
Question q6;
Question q7;
Question q8;
Question q9;
Question q10;
After that using the instances it will call the function of the
class “Question” .
8. It will call the function by passing the parameters which will set
the values to the class variables.
After that it will call another function which will displays the
Questions And waiting for the user to enter the option.
If the answer is correct it will calculate the score by adding the
score to the total.If the user entered answer is wrong it displays
the correct answer.
Finally if the total score of the quiz is greater than or equal to
70.then it displays a message that the user passed the exam.
If he got less score than 70 it displays the message that he
failed in the exam.
_____________________________________________________
____________________________________
Quiz.cpp
#include <iostream>
#include <string>
using namespace std;
//Declaring Global Variables which we can use any where in the
program.
int Guess;
int Total;
// Creating the class
class Question
{
9. private:
//declaring variables.
string Question_Text;
string Answer_1;
string Answer_2;
string Answer_3;
string Answer_4;
int Correct_Answer;
int Question_Score;
public:
//Functions.
void setValues (string,string,string,string,string, int, int);
void askQuestion();
};
int main()
{
//Displaying messages.
cout<<"Welcome to the Quiz Master"<<endl;
cout<<"Synonym & Antonym"<<endl;
cout<<"INSRUCTION"<<endl;
cout<<"*********************************************
**********"<<endl;
10. cout<<"* Choose one of the possible out of four. *"<<endl;
cout<<"* You have to get at least 70 or above *"<<endl;
cout<<"* to pass the quiz. *"<<endl;
cout<<"*********************************************
**********"<<endl;
cout<<"Press ENTER to start the quiz"<<endl;
// Wait until user press enter
cin.get();
string Name;
cout<<"What is your name?"<<endl;
cin>> Name;
string Respond;
cout<<"Are you ready to take the quiz, "<<Name<<"?
Yes/No?"<<endl;
cin>>Respond;
//If the user enters "yes" the if block will execute.If enters
"no" else block will execute.
if (Respond == "yes")
{
cout<<endl;
cout<<"Good Luck!"<<endl;
cout<<endl;
}
else if (Respond == "no")
11. {
cout<<"Goodbye!"<<endl;
return 0;
}
//Creating an instance to Question class.
Question q1;
Question q2;
Question q3;
Question q4;
Question q5;
Question q6;
Question q7;
Question q8;
Question q9;
Question q10;
//Calling the function of Question class by using the instance of
Question.
//Calling the Function by passing the parameters.
q1.setValues("what is the synonym of stupid?",
"dumb",
"smart",
"clever",
"genious",
1,
10);
12. //Calling the function of Question class by using the instance of
Question.
//Calling the Function by passing the parameters.
q2.setValues("what is the synonym of beautiful? ",
"helpful",
"happy",
"ugly",
"pretty",
4,
10);
//Calling the function of Question class by using the instance of
Question.
//Calling the Function by passing the parameters.
q3.setValues("what is the synonym of stretch?",
"fetch",
"intense",
"expand",
"tease",
3,
10);
//Calling the function of Question class by using the instance of
Question.
//Calling the Function by passing the parameters.
q4.setValues("what is the synonym of happy?",
"negative",
13. "impossible",
"joyful",
"hateful",
3,
10);
//Calling the function of Question class by using the instance of
Question.
//Calling the Function by passing the parameters.
q5.setValues("what is the synonym of good?",
"dream",
"excellent",
"recycle",
"mean",
2,
10);
//Calling the function of Question class by using the instance of
Question.
//Calling the Function by passing the parameters.
q6.setValues("what is the antonym of dark?",
"bright",
"green",
"dim",
"cloudy",
1,
10);
14. //Calling the function of Question class by using the instance of
Question.
//Calling the Function by passing the parameters.
q7.setValues("what is the antonym of sucessful?",
"stright",
"pass",
"fullfil",
"fail",
4,
10);
//Calling the function of Question class by using the instance of
Question.
//Calling the Function by passing the parameters.
q8.setValues("what is the antonym of tight?",
"tiny",
"small",
"loose",
"stress",
3,
10);
//Calling the function of Question class by using the instance of
Question.
//Calling the Function by passing the parameters.
q9.setValues("what is the antonym of advantage? ",
"benefit",
15. "disadvantage",
"favor",
"improvement",
2,
10);
//Calling the function of Question class by using the instance of
Question.
//Calling the Function by passing the parameters.
q10.setValues("what is the antonym of strong?",
"hard",
"soft",
"weak",
"power",
3,
10);
//Calling the Function by using the class instance.
q1.askQuestion();
q2.askQuestion();
q3.askQuestion();
q4.askQuestion();
q5.askQuestion();
q6.askQuestion();
q7.askQuestion();
q8.askQuestion();
q9.askQuestion();
16. q10.askQuestion();
//Displaying the score.
cout<<"Your total score is"<<Total<<"out of 100"<<endl;
cout<<endl;
//If the total score is greater than 70 the if block is executed.If
not else block is executed which displays Messages to the user.
if(Total >= 70)
{
cout<<"Great, you passed the quiz!"<<endl;
cout<<endl;
cout<<"Congratulation"<<endl;}
else
{
cout<<"Sorry, you failed the quiz"<<endl;
cout<<"Better luck next time!"<<endl;
}
return 0;
}
//Function Implementation Which will set the values to the
varibles declared in the class.
void Question::setValues(string q, string a1, string a2, string
a3, string a4, int ca, int qs)
17. {
Question_Text = q;
Answer_1 = a1;
Answer_2 = a2;
Answer_3 = a3;
Answer_4 = a4;
Correct_Answer = ca;
Question_Score = qs;
}
//Function implementation which displays Each Question And
wait for the user to Answer each question.
void Question:: askQuestion()
{
cout<<endl;
cout<< Question_Text <<endl;
cout<<"1."<<Answer_1<<endl;
cout<<"2."<<Answer_2<<endl;
cout<<"3."<<Answer_3<<endl;
cout<<"4."<<Answer_4<<endl;
cout<<endl;
18. cout<<"what is your answer?"<<endl;
cin>>Guess;
//If the user enters correct answer add 10(Question_score) to
total.
// This will repeat till user answered all questions.
//If the user enters wrong answer the else block will get
executed which displays the correct answer.
if (Guess == Correct_Answer)
{
cout<<endl;
cout<<"Correct!"<<endl;
Total = Total + Question_Score;
}
else
{
cout<<endl;
cout<<"Incorect!"<<endl;
cout<<"The correct answer is "<<
Correct_Answer<<"."<<endl;