Score calculation
- C++ course
- Intermediate
- Lesson 45/116
- 6 min
Learn correct count with the score calculation method.
Score calculation
Now we add score tracking to the Quiz class. An int score member is kept and incremented in run() for each question. A getScore() method returns the score.
Example: int score = 0; member variable. In run() we do score++; for each question. int getScore() { return score; } reads the score. In main we write std::cout << "Score: " << quiz.getScore();. In this example every question counts as correct, so the score equals the question count.
Score and member variable
The score member variable is part of the Quiz class. It increments each time run() is called; the object carries this state.
In this design score adds one point per question. A real quiz would check answers; but since this project has no input reading, we count every question as correct. The getScore() method exposes the score. This way we can print the result when the quiz is done.
Practice Add score tracking to Quiz
Take the previous lesson's Quiz class. After the questions member add int score = 0;. In run() after the std::cout line add score++;. Under public: add int getScore() with body return score;. In main after quiz.run(); add std::cout << "Score: " << quiz.getScore();.
Starter code
#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();
// Add score and a getScore method, print the score
return 0;
}Sample solution
#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();
return 0;
}Expected output
Quiz ready 2 questions added Question: Capital? Question: Largest planet? Score: 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does getScore() return?
int getScore() { return score; }- The question count
- The vector
- The score Correct answer
- An empty value
Explanation: getScore() returns the value of the score member.
2 Match each piece to its role
| Term | Meaning |
|---|---|
int score |
Score member |
score++ |
Increment |
getScore() |
Score reading |
return score |
Return value |
3 Complete the score member variable
class Quiz { public: std::vector<Question> questions; int ____ = 0; };
class Quiz { public: std::vector<Question> questions; int score = 0; };
Explanation: The score member is initialized to 0.