Kelick Tech Hub

Build, learn & keep up — tech, Linux & sports news included.

C++ Quiz Game Project

Beginner•3–4 hours•structs, vectors, scoring

A multiple-choice quiz that asks questions, checks answers, and grades you.

Share on X

What you will build

Load a set of multiple-choice questions, ask them one by one, and keep score. The interesting part isn't the asking — it's modelling a question cleanly (text, options, correct index) and separating the quiz data from the quiz engine so new questions need no new code.

What you will learn

Feature checklist

Starter code

#include <iostream>
#include <vector>
#include <string>

struct Question {
    std::string text;
    std::string options[4];
    char correct;  // 'A'..'D'
};

int main() {
    std::vector<Question> quiz = {
        {"Which keyword creates a class in C++?",
         {"struct", "class", "object", "define"}, 'B'},
        {"What does STL stand for?",
         {"Standard Template Library", "Simple Type List",
          "System Tool Library", "Static Text Language"}, 'A'},
    };
    int score = 0;
    for (const auto& q : quiz) {
        std::cout << "\n" << q.text << "\n";
        for (int i = 0; i < 4; ++i)
            std::cout << "  " << char('A' + i) << ") " << q.options[i] << "\n";
        char ans; std::cout << "Answer: "; std::cin >> ans;
        ans = std::toupper(ans);
        if (ans == q.correct) { ++score; std::cout << "Correct!\n"; }
        else std::cout << "No — it was " << q.correct << ".\n";
    }
    std::cout << "\nFinal score: " << score << "/" << quiz.size() << "\n";
}

Full walkthrough

Step 1: Model a question as a struct

struct Question { std::string text; std::array<std::string, 4> options; char answer; } — the question, its four options, and the letter of the correct one ('A'–'D'). A std::vector<Question> is the whole quiz bank. Data first, again: the game loop becomes trivial once the data shape is right.

Step 2: Load at least eight questions

The checklist asks for 8+ multiple-choice questions. Build them as an initializer list in code for the first version (the starter does this), and keep the answer letters balanced — if every answer is 'B', players will notice by question three and your shuffle will not save you.

Step 3: Shuffle the question order

Use std::shuffle from <algorithm> with the std::mt19937 engine you already know from the guessing game. Shuffling questions (not the options) is the checklist's bonus: it makes replays fresh without risking the answer letter pointing at the wrong option text.

Step 4: Ask, read a letter, judge immediately

For each question: print the text, print options labelled A) B) C) D), read a char, and compare against q.answer — converting the input to uppercase first, so 'b' and 'B' both count. Print instant feedback: 'Correct!' or 'Wrong — the answer was C.' Waiting until the end to reveal everything kills the game feel the checklist wants.

char given;
std::cin >> given;
given = std::toupper((unsigned char)given);
if (given == q.answer) { ++score; std::cout << "Correct!\n"; }
else std::cout << "Wrong — the answer was " << q.answer << ".\n";

Step 5: Track the score as you go

One int, incremented inside the correct branch. Because feedback is immediate, the running score is also feedback — print 'Score: 3/4' after each question so the player feels the stakes rising toward the final verdict.

Step 6: End with a percentage and a comment

percentage = score * 100 / total. Print 'You scored 7/8 (87%)' plus a tiered comment: 90%+ 'Outstanding!', 70%+ 'Great job', 50%+ 'Not bad — one more run?', below that 'Keep practising — the questions shuffle every game.' Integer maths is fine here as long as you multiply BEFORE dividing, or 7/8 becomes 0%.

Step 7: Test the judging, not just the questions

Deliberately answer every question wrong and confirm the score is 0 and every 'answer was' line names the right letter; then answer all right for 100%. A quiz whose answer key is off by one letter is worse than no quiz, and only this test catches it.

Common mistakes

Compile and run

g++ -std=c++17 -o quiz quiz.cpp

Sample run

$ ./quiz
Welcome to the quiz! 8 questions, shuffled every game.
Q1. Which keyword defines a class in C++?
A) struct  B) class  C) object  D) def
Your answer:
> B
Correct! Score: 1/1
Q2. What does the ++ operator do?
A) Adds two numbers  B) Doubles a value  C) Increments by one  D) Nothing
Your answer:
> C
Correct! Score: 2/2
Q3. Which header gives you std::vector?
A) <array>  B) <list>  C) <vec>  D) <vector>
Your answer:
> A
Wrong — the answer was D. Score: 2/3
(...5 more questions...)
You scored 7/8 (87%) — Great job!

Try it in your browser

No browser demo is included for this project. Build the C++ version below — that is the real program.

Related Beginner projects

← All 60 projects · Classic site (Learn & Sandbox)

Download Fake GPS