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

Create a project 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< Question> 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 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 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
Your answer?
a
Correct!

2. Who was the second president of the United States of America?
A. Abraham Lincoln
B. George Washington
C. Thomas Jefferson
D. John Adams
Your answer?
c
Wrong. The correct answer is D.

3. 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
  • 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< string> 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
      • Pushes the option onto the options vector
      • Sets answer and answerIndex if the option is the correct answer. Ex: If first option is the correct answer, 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. Comp Sci
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
Your answer?
d
Wrong. The correct answer is B.

3. 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
Your answer?
b
Correct!

4. 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
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!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.