CodeBlanks Logo Icon CodeBlanks

Running the quiz

Learn to ask questions in turn with the run method.

Running the quiz

Now we add a run() method to the Quiz class. This method prints each question's text to the screen. We loop over the questions.

Example: void run() { for (const auto& q : questions) { std::cout << "Question: " << q.text << std::endl; } }. Here for (const auto& q : questions) is a range-based for loop; it visits each Question element as q. In main calling quiz.run(); prints the questions. The question texts are added below the previous lessons' output.

Range-based for loop

A range-based for loop iterates over every element of a container. const auto& accesses each element by reference without copying.

Each Question in the questions vector has text and answer fields. We access the question text with q.text. In this lesson we print only the text; in the next lesson we will add answer checking and scoring. The output keeps growing each lesson.

Practice Add the run method to Quiz

Take the previous lesson's Quiz class. Add a void run() method under public:; the body is for (const auto& q : questions) { std::cout << "Question: " << q.text << std::endl; }. In main append << std::endl to the "questions added" line and call quiz.run();.

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;
    void addQuestion(std::string text, std::string answer) {
        questions.push_back({text, answer});
    }
};

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";
    // Add the run method and call it
    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;
    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;
        }
    }
};

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();
    return 0;
}

Expected output

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

Questions and answers

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

1 Match each piece to its role

Term Meaning
for (const auto& Range loop
q.text Question text
quiz.run() Run call
std::endl New line

2 Complete the range-based loop

Question
for (const auto& ____ : questions) { }
Answer
for (const auto& q : questions) { }

Explanation: q represents each Question element.

3 What does for (const auto& q : questions) do?

main.cpp
for (const auto& q : questions) { }
  • Visits only the first question
  • Visits each question one by one Correct answer
  • Clears the vector
  • Sorts the questions

Explanation: The range-based loop visits each element one by one.

All lessons in Intermediate