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 both size() 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

  1. Print a percent: 100.0 * score / quiz.size() as a double.
  2. Add a fifth question about vector and a matching given reply that is correct.
  3. If a given vector is shorter than the quiz, print an error and return from main without scoring.

Next project: a library catalog you can search by author.

FAQ: C++ Project: Quiz Game

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ quiz project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ quiz project in this C++ C++ lesson (C++ Project: Quiz Game).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.