Quiz Program in C++

               Let us create a game project in c++. It is a quiz program, which asks the player and make them choose one out of four options.

It saves the score and finally it displays the result.

C++ code:

#include <iostream>

#include <string>

using namespace std;

int main() {

    int score = 0;

    string ans;

    cout << "Welcome to the Game of Quiz!\n";

    cout << "--------------------------\n\n";

    // first question

    cout << "Q1: What is the capital of India?\n";

    cout << "a) New Delhi\nb) Madras\nc) Pondicherry\nd) Mumbai\n";

    cout << "choose Your answer: ";

    cin >> ans;

    if (ans == "a" || ans == "A") {

        cout << "Yes. You are Correct!\n\n";

        score++;

    } else {

        cout << "Wrong! The correct answer is New Delhi.\n\n";

    }

    // second Question

    cout << "Q2: Tell me the language used for Android app development?\n";

    cout << "a)IOS \nb)R  \nc)Java\nd) C++\n";

    cout << "What is Your answer?: ";

    cin >> ans;

    if (ans == "c" || ans == "C") {

        cout << "Correct!\n\n";

        score++;

    } else {

        cout << "Wrong! The correct answer is Java.\n\n";

    }

    // Third question

    cout << "Q3: Who invented World Wide Web?\n";

    cout << "a) Isaac Newton\nb) Berners Lee\nc) Charles\nd) Einstein\n";

    cout << "Give me Your answer: ";

    cin >> ans;

    if (ans == "b" || ans == "B") {

        cout << "Yes. You did it!\n\n";

        score++;

    } else {

        cout << "Wrong! The correct answer is Berners Lee.\n\n";

    }

    //Fourth question

    cout << "Q4: Who is the father of Computers?\n";

    cout << "a)james\nb)Sir.C.V.Raman\nc)Charles Babbage\nd) Newton\n";

    cout << "choose Your answer: ";

    cin >> ans;

    if (ans == "c" || ans == "C") {

        cout << "Yes. Your answer is Correct!\n\n";

        score++;

    } else {

        cout << "No.Your answer is Charles Babbage.\n\n";

    }

    // let us create the quiz score

    cout << "--------------------------\n";

    cout << "Quiz is finished! Your final score is: " << score << "/4\n";

    if (score == 4) {

        cout << "Excellent! You have done a great Job.\n";

    } else if (score == 3) {

        cout << "Good!Yet to excel.\n";

    } else if (score == 2) {

        cout << "Keep Practice.You have good chances\n";

    } else {

        cout << "Ok.Better Luck next time!\n";

    }

    return 0;

}

Output:

Welcome to the Game of Quiz!

--------------------------

Q1: What is the capital of India?

a) New Delhi

b) Madras

c) Pondicherry

d) Mumbai

choose Your answer: a

Yes. You are Correct!

 

Q2: Tell me the language used for Android app development?

a)IOS

b)R 

c)Java

d) C++

What is Your answer?: c

Correct!

 

Q3: Who invented World Wide Web?

a) Isaac Newton

b) Berners Lee

c) Charles

d) Einstein

Give me Your answer: b

Yes. You did it!

 

Q4: Who is the father of Computers?

a)james

b)Sir.C.V.Raman

c)Charles Babbage

d) Newton

choose Your answer: c

Yes. Your answer is Correct!

--------------------------

Quiz is finished! Your final score is: 4/4

Excellent! You have done a great Job.

Hope, you have understood the program. Keep Coding!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.