About – Quiz Game in C++
A Quiz Game is an engaging application that test a user’s knowledge across various through a series of question. Implementing a Quiz Game in C++ involves creating a structure to store questions and answers, functions to manage the game flow, and a user interface to interact with the player.
Features of the Quiz Game
- Questions and Answers:
- Each question includes the question text, multiple choice options, and the correct answer.
- Functionalities:
- Load Questions: Load a predefined set of questions and answers.
- Ask Questions: Present questions to the player one by one.
- Check Answers: Verify the player’s answer against the correct answer.
- Score Tracking: Keep track of the player’s score.
- Display Results: Show the final score at the end of the game.
- Data Management:
- Use arrays or vectors to store questions, options, and correct answers.
- Use loops and conditionals to manage the game flow.
C++ Codes for Project
#include <iostream> #include <vector> #include <string> class Question { private: std::string questionText; std::vector<std::string> options; char correctAnswer; public: Question(const std::string& qText, const std::vector<std::string>& opts, char cAnswer) : questionText(qText), options(opts), correctAnswer(cAnswer) {} void displayQuestion() const { std::cout << questionText << "\n"; for (size_t i = 0; i < options.size(); ++i) { std::cout << static_cast<char>('A' + i) << ". " << options[i] << "\n"; } } bool checkAnswer(char playerAnswer) const { return playerAnswer == correctAnswer; } char getCorrectAnswer() const { return correctAnswer; } }; class QuizGame { private: std::vector<Question> questions; int score; public: QuizGame() : score(0) {} void loadQuestions() { questions.emplace_back("What is the capital of France?", std::vector<std::string>{"Paris", "London", "Berlin", "Madrid"}, 'A'); questions.emplace_back("Which planet is known as the Red Planet?", std::vector<std::string>{"Earth", "Mars", "Jupiter", "Saturn"}, 'B'); questions.emplace_back("What is the largest ocean on Earth?", std::vector<std::string>{"Atlantic Ocean", "Indian Ocean", "Arctic Ocean", "Pacific Ocean"}, 'D'); } void start() { char playerAnswer; for (const auto& question : questions) { question.displayQuestion(); std::cout << "Your answer: "; std::cin >> playerAnswer; playerAnswer = toupper(playerAnswer); // Convert to uppercase for consistency if (question.checkAnswer(playerAnswer)) { std::cout << "Correct!\n"; ++score; } else { std::cout << "Wrong! The correct answer is " << question.getCorrectAnswer() << ".\n"; } std::cout << "---------------------\n"; } displayFinalScore(); } void displayFinalScore() const { std::cout << "Your final score is " << score << " out of " << questions.size() << ".\n"; } }; int main() { QuizGame quiz; quiz.loadQuestions(); quiz.start(); return 0; }