CodeBlanks Logo Icon CodeBlanks

Adding questions

Learn to add questions with the addQuestion method.

Adding questions

Now we add the ability to add questions to the Quiz class. We store questions in a std::vector<Question> and add new ones with an addQuestion method.

Example: void addQuestion(std::string text, std::string answer) { questions.push_back({text, answer}); }. Here push_back creates a new Question and appends it to the vector. In main you add a question with quiz.addQuestion("Capital?", "Ankara");. Below the previous lesson's "Quiz ready" output we print the question count.

vector and push_back

std::vector provides a dynamic array in C++. The push_back method appends an element; size() returns the element count.

We add a std::vector<Question> questions; member to the Quiz class from the previous lesson. We add two questions and print the count with questions.size(). This sets up the quiz's data structure. In the next lessons we will play the questions and calculate the score.

Practice Add the addQuestion method to Quiz

Take the previous lesson's Question and Quiz skeleton. Add a std::vector<Question> questions; member and a void addQuestion(std::string text, std::string answer) method under public: in Quiz; the body is questions.push_back({text, answer});. In main create a Quiz, add two questions and write std::cout << "Quiz ready" << std::endl; std::cout << quiz.questions.size() << " questions added";.

Starter code

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

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

class Quiz {
};

int main() {
    std::cout << "Quiz ready";
    // Add the addQuestion method, add two questions and print the count
    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});
    }
};

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

Expected output

Quiz ready
2 questions added

Questions and answers

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

1 Complete the addQuestion call

Question
quiz.____("Capital?", "Ankara");
Answer
quiz.addQuestion("Capital?", "Ankara");

Explanation: Questions are added with the addQuestion method.

2 Match each piece to its role

Term Meaning
std::vector<Question> Question list
push_back Append to end
size() Element count
addQuestion Adding method

3 What does push_back do?

main.cpp
questions.push_back({text, answer});
  • Appends an element to the end Correct answer
  • Shrinks the vector
  • Removes the first element
  • Returns an index

Explanation: push_back appends a new element to the end of the vector.

All lessons in Intermediate