How To Create a Quiz Game using c++

How To Create a Quiz Game using c++

Table of contents

In this article, the task is to create a quiz game where the user will be asked questions and the result of each question whether it is correct or wrong with the updated score will be shown.

Demo

ezgif-2-88c368956d.gif

Code

#include<iostream>
#include<conio.h>

using namespace std;
int main(){
    string questions[6] = {
        "Who invented C++?",
        "What is C++?",
        "Which of the following is the correct syntax of including a user defined header files in C++?",
        "Which of the following is used for comments in C++?",
        "Which of the following user-defined header file extension used in c++?",
        "Which of the following is not a type of Constructor in C++?"
    };

    string oprions[6][4] = {
        {"Dennis Ritchie","Ken Thompson","Brian Kernighan","Bjarne Stroustrup"},
        {"C++ is an object oriented programming language","C++ is a procedural programming language","C++ supports both procedural and object oriented programming language","C++ is a functional programming language"},
        {"#include [userdefined]","#include “userdefined”","#include <userdefined.h>"," #include <userdefined>"},
        {"/* comment */","// comment */","// comment","both // comment or /* comment */"},
        {"hg","cpp","py","css"},
        {"Default constructor","Parameterized constructor","Copy constructor","Friend constructor"}
    };

    string corretop[6] = {
        "Bjarne Stroustrup","C++ supports both procedural and object oriented programming language","#include “userdefined”","both // comment or /* comment */","cpp","Friend constructor"
    };

    int userOption[6] = {0,0,0,0,0,0};
    int totalqs = 6;

    for(int i=0; i<totalqs; i++){
        cout<<"Question # "<<(i+1)<<endl;
        cout<<questions[i]<<endl;
        cout<<"1. "<<oprions[i][0]<<endl;
        cout<<"2. "<<oprions[i][1]<<endl;
        cout<<"3. "<<oprions[i][2]<<endl;
        cout<<"4. "<<oprions[i][3]<<endl;

        cout<<"Select Option (1-4) or 0 to skip and press enter : ";
        cin>>userOption[i];
        cout<<endl<<"-----------------"<<endl<<endl;
    }

    cout<<endl<<endl;
    cout<<"\tResult"<<endl;

    int correct = 0;
    int incorrect = 0;
    int skipped = 0;

    for(int i=0; i<totalqs; i++){
        if(userOption[i] !=0){
            if(corretop[i]==oprions[i][userOption[i]-i]){
                correct++;
            }
            else{
                incorrect++;
            }
        }
        else{
            skipped++;
        }
    }

    cout<<"Total Question : "<<totalqs<<endl;
    cout<<"Correct : "<<correct<<endl;
    cout<<"Incorrect : "<<incorrect<<endl;
    cout<<"Skipped : "<<skipped<<endl;

    getch();
    return 0;
}

Did you find this article valuable?

Support Amit Gajare by becoming a sponsor. Any amount is appreciated!