Build a Quiz App as shown to you in the class.
Use Struct for this purpose.
1)Each question should print one by one.
2)We can select one option
-> If the selected option is correct print "correct answer shabash" and add 5 marks to the total marks
-> If the selected option is wrong then print "Upps Wrong Answer Naliak"
3) At the end of the quiz you should printout Total marks obtained and on the base of those marks tell the candidate if he/she has paased or fail the Test
#include <iostream>
#include <string>
using namespace std;
struct Quiz{
string question;
string answers[4];
int correctAnswer;
};
int main(){
int totalMarks=0;
Quiz quiz[5];
quiz[0].question="The default access specifer for the class members is";
quiz[0].answers[0]="public";
quiz[0].answers[1]="private";
quiz[0].answers[2]="protected";
quiz[0].answers[3]="None of the above.";
quiz[0].correctAnswer=2;
quiz[1].question="A trigraph character begins with";
quiz[1].answers[0]="#";
quiz[1].answers[1]="##";
quiz[1].answers[2]="?";
quiz[1].answers[3]="??";
quiz[1].correctAnswer=3;
quiz[2].question="C++ does not supports the following";
quiz[2].answers[0]="Multilevel inheritance";
quiz[2].answers[1]="Hierarchical inheritance";
quiz[2].answers[2]="Hybrid inheritance";
quiz[2].answers[3]="None of the above.";
quiz[2].correctAnswer=4;
quiz[3].question="One of the following is true for an inline function.";
quiz[3].answers[0]="It executes faster as it is treated as a macro internally";
quiz[3].answers[1]="It executes faster because it priority is more than normal function";
quiz[3].answers[2]="It doesn’t executes faster compared to a normal function";
quiz[3].answers[3]="None of the above holds true for an inline function";
quiz[3].correctAnswer=1;
quiz[4].question="Choose the pure virtual function definition from the following.";
quiz[4].answers[0]="virtual void f()=0 { }";
quiz[4].answers[1]="void virtual f()=0 { }";
quiz[4].answers[2]="virtual void f() {} = 0;";
quiz[4].answers[3]="None of the above.";
quiz[4].correctAnswer=4;
int userAnswer;
//1)Each question should print one by one.
for(int i=0;i<5;i++){
cout<<"Q "<<(i+1)<<" - "<<quiz[i].question<<"\n";
for(int j=0;j<4;j++){
cout<<(j+1)<<" - "<<quiz[i].answers[j]<<"\n";
}
//2)We can select one option
cout<<"Your choice: ";
cin>>userAnswer;
//-> If the selected option is correct print "correct answer shabash" and add 5 marks to the total marks
if(userAnswer==quiz[i].correctAnswer){
cout<<"\nCorrect answer shabash\n\n";
totalMarks+=5;
}else{
//-> If the selected option is wrong then print "Upps Wrong Answer Naliak"
cout<<"\nUpps Wrong Answer Naliak\n\n";
}
}
//3) At the end of the quiz you should printout Total marks obtained and on the base of those marks tell the candidate if he/she has paased or fail the Test
cout<<"\nTotal marks: "<<totalMarks<<"\n";
if(totalMarks>=20){
cout<<"The student pased the test\n\n";
}else{
cout<<"The student fail the test\n\n";
}
int k;
cin>>k;
return 0;
}
Comments
Leave a comment