×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Multiple Choice quiz using C++ Assignment Solution

June 24, 2024
Joseph
Joseph
🇨🇦 Canada
C++
Joseph, with a master’s in computer science from the University of Regina, is an expert in C++ homework, boasting 9 years of experience in the field.
Key Topics
  • Program: Quizzy
Tip of the day
Ensure you understand the dataset thoroughly before starting your machine learning assignment. Visualize the data, check for missing values, and identify patterns or anomalies to guide your model-building process effectively.
News
In 2024, universities have introduced new programming courses focusing on cybersecurity, machine learning, and artificial intelligence to better prepare students for modern IT careers.

Program: Quizzy

Overview

Create a program called Quizzy that allows the user to take multiple-choice quizzes. The quiz questions and answers are given in text files.

The quizlist.txt file specifies the name of each quiz (one name per line). Ex:

History

Movies

Comp Sci

There can be any number of quizzes in quizlist.txt.

Each quiz's text file is named after the quiz by appending a ".txt" file extension. Ex: The History quiz's questions are in History.txt. The quiz file contains a question (on a single line) followed by exactly 4 possible answers (called options). The correct answer has a ^ at the beginning. The file may contain any number of questions.

Ex History.txt file:

In what year was the Declaration of Independence signed?

^1776

1670

1782

1882

Who was the second president of the United States of America?

Abraham Lincoln

George Washington

Thomas Jefferson

John Adams

What city was destroyed in 79 AD by a volcano on Mount Vesuvius?

Rome

Florence

Pompeii

Venice

Project organization

Write a C++ assignment with the following files:

  • Quiz.h - Class declaration
  • Quiz.cpp - Class definition
  • Question.h - Class declaration
  • Question.cpp - Class definition
  • main.cpp - Contains main() function
  • Quiz class

The Quiz class should contain the following:

  • Default constructor
  • Overloaded constructor that takes the quiz name as a parameter
  • Private data members
    • string name - Initialized in default constructor to "NO NAME"
    • vector questions
  • Public member functions
    • GetName()
    • SetName()
    • NumQuestions()
  • Returns number of questions in questions vector
    • AddQuestion()
  • Adds the Question parameter to questions vector
    • TakeQuiz()
  • Displays the quiz name
  • Asks the user all the questions in the questions vector
  • Indicates if the answer is correct. If not, displays the correct answer
  • User input is case insensitive (Ex: B and b are acceptable to choose option B)
  • Displays total correct answers when the quiz is completed

Example of TakeQuiz() output that includes user's input:

History Quiz

  1. In what year was the Declaration of Independence signed?
    • A. 1776
    • B. 1670
    • C. 1782
    • D. 1882
  2. Your Answer

    A.

    Correct

  3. Who was the second president of the United States of America?
    • A. Abraham Lincoln
    • B. George Washington
    • C. Thomas Jefferson
    • D. John Adams
  4. Your Answer

    C.

    Wrong. The correct answer is D.

  5. What city was destroyed in 79 AD by a volcano on Mount Vesuvius?
    • A. Rome
    • B. Florence
    • C. Pompeii
    • D. Venice

    Your Answer?

    C.

    Correct

You answered 2 of 3 questions correct!

Question class

The Question class should contain the following:

  • Default constructor
  • An overloaded constructor that takes the question text as a parameter
  • Private data members
    • string text - Initialized in default constructor to "NO TEXT"
    • char answer - Initialized in both constructors to '?'
    • int answerIndex - Initialized in both constructors to -1
    • vector options
  • Public member functions
    • GetText()
  • Returns the question text
    • AddOption()
  • First parameter is the option string, second is a bool that indicates if the option is the correct answer
  • First parameter is the option string, second is a bool that indicates if the option is the correct answer
  • Sets answer and answerIndex if the option is the correct answer. Ex: If the first option is the correct answer, the answer is assigned 'A', and answerIndex is assigned 0
    • GetOption()
  • Returns the string from the options vector using the option index parameter where 0 = first option, 3 = last option
  • If option index parameter is out of range (< 0 or > 3) then returns "UNKNOWN"
    • GetAnswer()
  • Returns the answer (Ex: 'B')

main.cpp

The main.cpp file should read the quiz names from quizlist.txt, display the quiz names, and prompt the user to enter a quiz to take

  • If the user enters 0, the program should terminate.
  • If the user enters an invalid number, main. cpp should re-display the quiz list and re-prompt the user.
  • If the user selects a valid quiz to take, main. CPP should create a Quiz object by reading the appropriate quiz file, creating Question objects from the file, and adding the Question objects to the Quiz object. Then main. cpp should call the Quiz member function TakeQuiz() so the user can take the quiz. After the quiz is completed, the quiz list should be displayed again so the user can take another quiz.

Example program output with user input displayed:

Welcome to Quizzy

  1. History
  2. Movies
  3. Movies

Which quiz would you like to take (0 to quit)?

2

Movies Quiz

  1. What was the first feature-length animated movie ever released?
    • A. Snow White and the Seven Dwarfs
    • B. Pinocchio
    • C. Fantasia
    • D. Dumbo

    Your Answer?

    A.

    Correct!

  2. What does Neo do to discover the truth of The Matrix?
    • A. Gets hit by lightning
    • B. Swallows red pill
    • C. Injected with truth serum
    • D. Jumps off a building
  3. Your Answer?

    D.

    Wrong. The correct answer is B.

  4. For what movie did Steven Spielberg win his first Oscar for Best Director?
    • A. Ready Player One
    • B. Schindler's List
    • C. Indiana Jones
    • D. Jurassic Park
  5. Your Answer?

    B.

    Correct!

  6. What song plays over the opening credits of Guardians of the Galaxy?
    • A. "Hooked on a Feeling" by Blue Swede
    • B. "Go All the Way" by Raspberries
    • C. "Cherry Bomb" by The Runaways
    • D. "Come and Get Your Love" by Redbone
  7. Your Answer?

    A.

    Wrong. The correct answer is D.

You answered 2 of 4 questions correct!

  1. History
  2. Movies
  3. Comp Sci

Which quiz would you like to take (0 to quit)?

4

  1. History
  2. Movies
  3. Comp Sci

Which quiz would you like to take (0 to quit)?

0

GoodBye

Solution

Main

#include < iostream> #include < string> #include < fstream> #include < vector> #include < cstdlib> #include " Question.h" #include " Quiz.h" using namespace std; // Load the questions of a quiz void loadQuestions(vector &questions, const string &filename) { ifstream inFile(filename.c_str()); if (!inFile.is_open()) { cout << "Failed to open " << filename << endl; exit(0); } string text; // Read a question while (getline(inFile, text)) { // Expect 4 choices Question question(text); for (int i = 0; i < 4; i++) { string option; getline(inFile, option); bool isAnswer = false; if (option[0] == '^') { isAnswer = true; option = option.substr(1); } question.addOption(option, isAnswer); } questions.push_back(question); } } // Load all quizes as objects from file void loadQuizList(vector &quizzes) { ifstream inFile("quizlist.txt"); if (!inFile.is_open()) { cout << "Failed to open quizlist.txt" << endl; exit(0); } string name; // Read a quiz while (getline(inFile, name)) { // Load the quiz questions from another file string filename = name + ".txt"; vector questions; loadQuestions(questions, filename); Quiz quiz(name, questions); quizzes.push_back(quiz); } inFile.close(); } // Entry point of the program int main() { // Load the quizzes vector quizzes; loadQuizList(quizzes); cout << "Welcome to Quizzy" << endl; cout << endl; while (true) { // Let the user decide which quiz to take for (int i = 0; i < (int)quizzes.size(); i++) cout << (i + 1) << ". " << quizzes[i].getName() << endl; cout << "Which quiz would you like to take (0 to quit)?" << endl; int option; cin >> option; // Quit if user wants to if (option == 0) break; cout << endl; // Validate and load the quiz option--; if (option >= 0 && option < (int)quizzes.size()) { quizzes[option].takeQuiz(); cout << endl; } } cout << "Goodbye!" << endl; return 0; }

Question

#include "Question.h" // Create a question Question::Question() { text = ""; answer = '?'; answerIndex = -1; } // Create the question Question::Question(const string &text) { this->text = text; answer = '?'; answerIndex = -1; } // Access to the text property string Question::getText() const { return text; } // Add a new option for the question void Question::addOption(const string &option, bool isAnswer) { options.push_back(option); if (isAnswer) { answerIndex = options.size() - 1; answer = (char)('A' + answerIndex); } } // Get the option text at the given index string Question::getOption(int index) const { if (index < 0 || index >= (int)options.size()) return "UNKNOWN"; return options[index]; } // Get the answer to the question char Question::getAnswer() const { return answer; }

Similar Samples

Our sample projects showcase the high-quality work our experts deliver. Browse through to see real examples of our programming solutions, demonstrating our commitment to excellence and the depth of our expertise across various languages and frameworks. Quality and reliability are our top priorities.