Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This assignment has one part You will be creating a trivia game using classes. The name of your program source file should be Assignment3.cpp. You

This assignment has one part You will be creating a trivia game using classes. The name of your program source file should be Assignment3.cpp. You will also need .cpp and .h files for your classes. Building the Trivia Game Write a program that implements a trivia game. The program will need two classes. Question class The Question class represents one question and the possible answers for that question. The Question class contains the question text and the 4 possible answers. It also contains the answer number of the correct answer. The answer number is an unsigned int in the range of 1 to 4. Your Question class needs to have one or more constructors and one destructor. It needs a member function that returns the question text. It also needs member functions that return the answer text for a specific answer (1 through 4). The class also needs a member function that returns the number of possible answers for this question. While this is 4 today, it could change in future versions of the class. Finally there needs to be a member function that returns the answer number of the correct answer (a value of 1 through 4). Do not make the data members public. You can have public and private member functions as needed. TriviaGame class The TriviaGame class needs to be able to create a game from an input text file. The name of the input file needs to be passed as a parameter to the TriviaGame class constructor. The number of questions will always be an even number that is 2 or more. The TriviaGame class needs to create a dynamically allocated array of Question objects. The TriviaGame class needs public member functions that will return the number of questions for each player (each player is asked half of the questions). The class also needs to return the next question for a given player (this should return a const reference to the next Question object for the given player).

The class needs to have two public member functions, one for each player, that will determine if the answer selected by the user is the correct answer for the question. If it is the valid answer the member function needs to return true and increment the points for that student. Today each question is worth 1 points. This could change in the future, so the addition of the points should be done using a member function in the TriviaGame class. Member functions are also needed to return the current points for a specific player. Make sure you delete the dynamically allocated array in the classs destructor. Do not make the data members public, they should all be private. Assignment 3 main function: The main function needs to create an instance of the TriviaGame class and play the game. For each round the program needs to display the question for player 1 and get the answer to the question from player 1. It will use the TriviaGame object to get question and then display the text of the question and the various answers. It will then use the appropriate member function of the TriviaGame class to check to see if the specified answer was the correct one. It will then do the same processing for the next question for player 2. These rounds will continue until there are no more questions. After all questions have been answered the main function should determine the winner and output the number of points each player received. Feel free to create additional functions to implement your menu and answer logic.

Input file format: The sample file triviaGame1.txt has been provided to you. Feel free to create and user your own files.

4

Which planet in the solar system is closest to the Sun?

Mercury

Mars

Earth

Venus

1

What was the longest running prime time scripted TV show in the US?

Gunsmoke

South Park

The Jack Benny Program

The Simpsons 4

Which US President was never elected as either President or Vice President?

George Washington

Gerald Ford

Theodore Roosevelt

Millard Filmore

2

Who was the 4th person to walk on the moon?

John Young

Alan Bean

Alan Shepard

Jim Lovell 2

The first value in the file (4 above) is the total number of questions and answer sets that follow. Each player will be asked half of these questions. The next input line (Which planet in the solar system is closest to the Sun? above) is the text for question # 1. You need to use getline to read this in.

The next 4 input lines are the answers to the question. Again, you can use getline to read in these lines of text. Next is the number 2. This indicates that the 2nd answer (Gerald Ford) is the correct answer. The answer number is 1 based with the first answer being answer 1, the second answer being answer 2, and so on. This sequence of question text, the text for 4 answers, and the correct answer are repeated for each of the specified questions (the first value read in, 4 above at the very top of the file). You will probably be using a mix of the >> operator and getline to read in the values. When you are switching from the >> operator to getline you will need to clear out any data in the input buffer left over from the >> operator. Source Files: When you submit your assignment3.cpp file make sure it uses file triviaGame1.txt. You also need to create .cpp and .h files for the Question and TriviaGame classes. You can either create these in the same .h and file or in different .h files. You can use inline member functions for some of the member functions in your classes, but some of the member functions MUST NOT BE inline functions and must be in a .cpp file other than assignment3.cpp. Here is some sample output for the game in file triviaGame1.txt (your output can differ, but you must have the equivalent information): This game consists of 2 players, each answering 2 questions. The player with the most points will win the game. Question # 1 for player 1

Which planet in the solar system is closest to the Sun?

Pick from the following answers:

A # 1: Mercury

A # 2: Mars

A # 3: Earth

A # 4: Venus

Enter your answer: 1[Enter]

Question # 1 for player 2

Which US President was never elected as either President or Vice President?

Pick from the following answers:

A # 1: George Washington

A # 2: Gerald Ford

A # 3: Theodore Roosevelt

A # 4: Millard Filmore

Enter your answer: 2[Enter]

Player 1 had the correct answer for question 1

Player 2 had the correct answer for question 1

Question # 2 for player 1

What was the longest running prime time scripted TV show in the US?

Pick from the following answers: A # 1: Gunsmoke A # 2: South Park A # 3: The Jack Benny Program CS1337.013 Assignment 3 5 A # 4: The Simpsons Enter your answer: 2[Enter] Question # 2 for player 2 Who was the 4th person to walk on the moon? Pick from the following answers: A # 1: John Young A # 2: Alan Bean A # 3: Alan Shepard A # 4: Jim Lovell Enter your answer: 2[Enter] Player 1 had the incorrect answer for question 2 Player 2 had the correct answer for question 2 Player 2 wins with 2 points, player 1 received 1 points.

Source file ?triviaGame1.txt?? Which planet in the solar system is closest to the Sun? Mercury Mars Earth Venus 1 What was the longest running prime time scripted TV show in the US? Gunsmoke South Park The Jack Benny Program The Simpsons 4 Which US President was never elected as either President or Vice President? George Washington Gerald Ford Theodore Roosevelt Millard Filmore 2 Who was the 4th person to walk on the moon? John Young Alan Bean Alan Shepard Jim Lovell 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions