C++ Tutorial
C++ Project: Quiz Game
A scored quiz from a vector of questions. Print each prompt, check the answer, and tally.
What you will build
A quiz is a list of prompts and the string that counts as correct. You print a question, compare a given answer, add one to the score on a match, then print a short report. The list is avector<Question>. The given answers in this tutorial are a fixed sequence so the output is the same every run.
That fixed sequence is the point of the demo: you can read the tally and know which items were right. ClickTry it in C++ to compile at /cpp/try. Do not paste this into Python or HTML.
A Question struct
Each item needs a prompt and an answer. Keep both as string so a correct reply can be a word, not only a letter. One struct is one question. The quiz will hold several.
Example
#include <iostream>
#include <string>
using namespace std;
struct Question {
string prompt;
string answer;
};
int main() {
Question q;
q.prompt = "How many bits in a byte?";
q.answer = "8";
cout << q.prompt << endl;
cout << "Answer: " << q.answer << endl;
return 0;
}Store the quiz in a vector
Push each question onto a vector. A loop can then print every prompt. The length of the quiz issize(), so adding a question does not require a new array bound.
Example
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Question {
string prompt;
string answer;
};
int main() {
vector<Question> quiz;
quiz.push_back({"How many bits in a byte?", "8"});
quiz.push_back({"C++ header for cout?", "iostream"});
quiz.push_back({"Keyword for a class field that callers cannot touch?", "private"});
for (size_t i = 0; i < quiz.size(); i++) {
cout << (i + 1) << ". " << quiz[i].prompt << endl;
}
return 0;
}Use Try it in C++ so this list opens in /cpp/try. Add a fourth prompt withpush_back and confirm the numbering still starts at 1.
Check one answer
Compare the given string to question.answer. Exact match scores a point. This demo keeps comparisons case-sensitive so you see when a reply is almost right but not counted. A helper returns 1 or 0 so the tally is a running sum.
Example
#include <iostream>
#include <string>
using namespace std;
struct Question {
string prompt;
string answer;
};
int mark(const Question& q, const string& given) {
if (given == q.answer) {
cout << "Correct" << endl;
return 1;
}
cout << "Wrong (expected " << q.answer << ")" << endl;
return 0;
}
int main() {
Question q{"Capital of France?", "Paris"};
cout << q.prompt << endl;
int score = 0;
score = score + mark(q, "Paris");
score = score + mark(q, "paris");
cout << "Score " << score << endl;
return 0;
}First given answer matches. Second does not, because P and p differ. Score is 1.
Complete quiz with a fixed sequence
Pair each question with a given reply in a second vector of the same length. Walk both with one index. Never call cin here: the sequence is part of the program so the report is deterministic. A later version can fill given from the keyboard.
Example
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Question {
string prompt;
string answer;
};
int mark(const Question& q, const string& given) {
cout << "You: " << given << endl;
if (given == q.answer) {
cout << "Correct" << endl;
return 1;
}
cout << "Wrong (expected " << q.answer << ")" << endl;
return 0;
}
int main() {
vector<Question> quiz;
quiz.push_back({"How many bits in a byte?", "8"});
quiz.push_back({"C++ header for cout?", "iostream"});
quiz.push_back({"Keyword for a class field that callers cannot touch?", "private"});
quiz.push_back({"2 + 2?", "4"});
vector<string> given;
given.push_back("8");
given.push_back("stdio.h");
given.push_back("private");
given.push_back("4");
int score = 0;
for (size_t i = 0; i < quiz.size(); i++) {
cout << (i + 1) << ". " << quiz[i].prompt << endl;
score = score + mark(quiz[i], given[i]);
}
cout << "Score: " << score << " / " << quiz.size() << endl;
return 0;
}Question 2 is wrong on purpose: the C++ header is iostream, not stdio.h. The report is Score: 3 / 4 every time you run this file.
Common mistakes
- Letting the given-answer vector run shorter than the quiz. Then
given[i]walks off the end. Check that bothsize()values match, or store the reply on the struct. - Scoring with
=instead of==. Assignment does not compare strings. - Printing the expected answer before the player replies, then wondering why the quiz is not a quiz. Keep the key inside the struct and print it only on a miss, as above, or hide it until you add typed input.
- Mixing spaces in answers.
"Paris "with a trailing space is not"Paris".
Practice
- Print a percent:
100.0 * score / quiz.size()as adouble. - Add a fifth question about
vectorand a matching given reply that is correct. - If a given vector is shorter than the quiz, print an error and return from
mainwithout scoring.
Next project: a library catalog you can search by author.