Metadata

  • πŸ“… Date :: 12-06-2025
  • 🏷️ Tags :: cpp project

Notes

Detailed Notes on Creating a Quiz Game in C++

In this guide, we are going to walk through how to create a quiz game in C++ where users can answer multiple-choice questions. The game will have a set of questions and corresponding options, along with an answer key to check if the user’s answers are correct.

1. Define Arrays for Questions and Options

To store the questions and their multiple-choice options, two arrays are used:

  • Questions Array: Stores the actual quiz questions.
  • Options Array: A two-dimensional array stores the options corresponding to each question. This 2D array has a row for each question and four columns (one for each option).

Example:

// Array of questions
string questions[] = {
    "What year was C++ created?",
    "Who invented C++?",
    "What is the predecessor of C++?",
    "Is the Earth flat?"
};
 
// 2D Array of options corresponding to each question
string options[][4] = {
    {"A) 1969", "B) 1975", "C) 1985", "D) 1989"},
    {"A) Guido Van Rossum", "B) Bjorn Stroustrup", "C) John Carmack", "D) Mark Zuckerberg"},
    {"A) C", "B) C++", "C) C- (C Minus Minus)", "D) C++" },
    {"A) Yes", "B) No", "C) Sometimes", "D) What is Earth?"}
};
 

2. Answer Key

An array holds the correct answers for each question:

  • The Answer Key Array stores the correct option (A, B, C, or D) for each question.

Example:

char answerKey[] = {'C', 'B', 'A', 'B'}; // Correct answers for each question
 

3. Calculate the Size of the Questions Array

We use the sizeof() operator to calculate the number of questions in the array.

Example:

int size = sizeof(questions) / sizeof(questions[0]); // Number of questions
 

4. Iterating through Questions and Options

  • We use a for loop to iterate through each question.
  • For each question, we display the question and its four options.

Example:

for (int i = 0; i < size; i++) {
    cout << "Question " << (i + 1) << ": " << questions[i] << endl;
 
    // Nested loop to display options for the current question
    for (int j = 0; j < 4; j++) {
        cout << options[i][j] << endl;
    }
 
    // Accept user input for the answer
    char guess;
    cout << "Your answer: ";
    cin >> guess;
 
    // Normalize to uppercase for consistency
    guess = toupper(guess);
 

5. Checking User’s Answer

  • Once the user enters their guess, we compare it with the correct answer from the answer key.
  • If the guess is correct, we increase the score.

Example:

if (guess == answerKey[i]) {
    cout << "Correct!" << endl;
    score++;  // Increase score if answer is correct
} else {
    cout << "Wrong! The correct answer is " << answerKey[i] << endl;
}
 

6. Displaying the Results

After all the questions are answered, we display the results, including the number of correct answers, total questions, and the score percentage.

  • Score Calculation: The percentage is calculated as:
double percentage = (score / (double)size) * 100;
 

We cast the number of questions (size) to double to ensure that the division results in a floating-point number and includes the decimal portion.

Example:

cout << "Results:" << endl;
cout << "Correct guesses: " << score << endl;
cout << "Number of questions: " << size << endl;
cout << "Your score: " << percentage << "%" << endl;
 

7. Typecasting to Handle Decimal Precision

In the percentage calculation, integer division would truncate the decimal portion, so we use typecasting to ensure that the result is a floating-point number. The line:

(double)size
 

is used to cast the size to double so that the division gives the correct decimal result.

8. Example Output

Here’s how the output looks when running the quiz:

Question 1: What year was C++ created?
A) 1969
B) 1975
C) 1985
D) 1989
Your answer: C
Correct!

Question 2: Who invented C++?
A) Guido Van Rossum
B) Bjorn Stroustrup
C) John Carmack
D) Mark Zuckerberg
Your answer: B
Correct!

Question 3: What is the predecessor of C++?
A) C
B) C++
C) C- (C Minus Minus)
D) C++
Your answer: A
Correct!

Question 4: Is the Earth flat?
A) Yes
B) No
C) Sometimes
D) What is Earth?
Your answer: B
Correct!

Results:
Correct guesses: 4
Number of questions: 4
Your score: 100%

9. Improvement Opportunities

  • Input Validation: Add input validation to ensure that the user enters only valid options (A, B, C, or D). If the user enters an invalid option, prompt them to enter a valid answer again.
  • Dynamic Questions: Allow for dynamic question loading, where users can add their questions and options from an external file or dynamically generate them.

10. Final Thoughts

This quiz game is a simple console-based application in C++ that demonstrates several key concepts:

  • Arrays (1D and 2D)
  • User input handling
  • Loops for iteration
  • Conditional statements for checking answers
  • Basic arithmetic and typecasting

This game can be modified and expanded upon by adding more questions, providing a timer, or implementing difficulty levels.


References