CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine the Quiz class with a console menu.

Putting it together

In the final lesson we add a reset() method to the Quiz class and complete the app. reset() zeroes the score so the quiz can be restarted.

All pieces are now together: the Question struct, addQuestion, run, getScore and reset methods. main creates a quiz, adds questions, runs it, prints the score and resets. This is a cumulative project example: each lesson was built on top of the previous one.

Project completed

You completed the project! Over five lessons you built a Quiz class step by step. This is a small example of real-world software development.

You first defined the data model (Question), then added methods for adding questions, running, scoring and resetting. Each lesson's starting code contained the previous solution, so the code grew cumulatively. This approach shows the power of breaking large projects into small steps.

Practice Add the reset method to Quiz

Take the previous lesson's Quiz class. After getScore() add void reset() with body score = 0;. In main append << std::endl to the std::cout << "Score: " << quiz.getScore(); line, then add quiz.reset(); and std::cout << "Reset";.

Starter code

main.cpp
#include <iostream>
#include <string>
#include <vector>

struct Question {
    std::string text;
    std::string answer;
};

class Quiz {
public:
    std::vector<Question> questions;
    int score = 0;
    void addQuestion(std::string text, std::string answer) {
        questions.push_back({text, answer});
    }
    void run() {
        for (const auto& q : questions) {
            std::cout << "Question: " << q.text << std::endl;
            score++;
        }
    }
    int getScore() {
        return score;
    }
};

int main() {
    Quiz quiz;
    quiz.addQuestion("Capital?", "Ankara");
    quiz.addQuestion("Largest planet?", "Jupiter");
    std::cout << "Quiz ready" << std::endl;
    std::cout << quiz.questions.size() << " questions added" << std::endl;
    quiz.run();
    std::cout << "Score: " << quiz.getScore();
    // Add the reset method and call it, print a message
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <string>
#include <vector>

struct Question {
    std::string text;
    std::string answer;
};

class Quiz {
public:
    std::vector<Question> questions;
    int score = 0;
    void addQuestion(std::string text, std::string answer) {
        questions.push_back({text, answer});
    }
    void run() {
        for (const auto& q : questions) {
            std::cout << "Question: " << q.text << std::endl;
            score++;
        }
    }
    int getScore() {
        return score;
    }
    void reset() {
        score = 0;
    }
};

int main() {
    Quiz quiz;
    quiz.addQuestion("Capital?", "Ankara");
    quiz.addQuestion("Largest planet?", "Jupiter");
    std::cout << "Quiz ready" << std::endl;
    std::cout << quiz.questions.size() << " questions added" << std::endl;
    quiz.run();
    std::cout << "Score: " << quiz.getScore() << std::endl;
    quiz.reset();
    std::cout << "Reset";
    return 0;
}

Expected output

Quiz ready
2 questions added
Question: Capital?
Question: Largest planet?
Score: 2
Reset

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each method to its purpose

Term Meaning
addQuestion Add a question
run Run the quiz
getScore Read the score
reset Reset

2 What does the reset() method do?

main.cpp
void reset() { score = 0; }
  • Hides the methods
  • Deletes the questions
  • Closes the program
  • Resets the score Correct answer

Explanation: reset() sets the score variable to 0.

3 Complete the reset call

Question
quiz.____();
Answer
quiz.reset();

Explanation: quiz.reset(); zeroes the score.

All lessons in Intermediate