the main problem i have with this project is getting the "trivia.txt" file to be printed and stream correctly into my program. thanks. this is for c++ thanks
In this programming challenge, you will create a simple trivia game for two players. The program will work like this: Starting with player 1, each player gets a turn at answering five trivia questions. (There are a total of 10 questions.) When a question is displayed, four possible answers are also displayed. Only one of the answers is correct, and if the player selects the correct answer, he or she earns a point. After answers have been selected for all of the questions, the program displays the number of points earned by each player and declares the player with the highest number of points the winner. The program must load the questions from a text file in to the array of 10 Question objects. In this program, you will design a Question class to hold the data for a trivia question. The Question class should have member variables for the following data:
A trivia question
Possible answer #1
Possible answer #2
Possible answer #3
Possible answer #4
The number of the correct answer (1, 2, 3, or 4)
The Question class should have appropriate constructor(s), accessor, and mutator functions.
The program should create an array of 10 Question objects, one for each trivia question. Make up your own questions on the subject or subjects of your choice for the objects.
this is what the txt file should contain. (copy past the text below and past it in a txt.file)
(1) What famous document begins: "When in the course of human events..."?
The Gettysburg Address
The US Declaration of Independence
The Magna Carta
The US Bill of Rights
2
(2) Who said "A billion dollars isn't worth what it used to be"?
J. Paul Getty
Bill Gates
Warren Buffet
Henry Ford
1
(3) What number does "giga" stand for?
One thousand
One million
One billion
One trillion
3
(4) What number is 1 followed by 100 zeros?
A quintillion
A googol
A moogle
A septaquintillion
2
(5) Which of the planets is closest in size to our moon?
Mercury
Venus
Mars
Jupiter
1
(6) What do you call a group of geese on the ground?
skein
pack
huddle
gaggle
4
(7) What do you call a group of geese in the air?
skein
pack
huddle
gaggle
1
(8) Talk show host Jerry Springer was the mayor of this city.
Chicago
Indianapolis
Cincinnati
Houston
3
(9) On a standard telephone keypad, the letters T, U, and V are matched to
what number?
5
6
7
8
4
(10) Crickets hear through this part of their bodies.
Head
Knees
Ears
Tail
2
I have solved the above problem for you. I have taken care of all the requirements specified in the problem.
The Code is given below:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Question
{
string trivia;//variable used for trivia
question
string option1;//first probable answer
string option2;//second probable answer
string option3;//third probable answer
string option4;//fourth probable answer
string answer; //correct option
public:
Question() //constructor used to initialize data
members of this class
{
trivia="";
option1="";
option2="";
option3="";
option4="";
answer="";
}
void set_trivia(string str)//method used to set
trivia question
{
this->trivia=this->trivia+str;
}
string get_trivia()//method to access the trivia
question
{
return
this->trivia;
}
void set_option1(string str)//method used to set
first probable answer
{
this->option1=str;
}
string get_option1()//method to access the first
probable answer
{
return
this->option1;
}
void set_option2(string str)//method used to set
second probable answer
{
this->option2=str;
}
string get_option2()//method to access the
second probable answer
{
return
this->option2;
}
void set_option3(string str)//method used to set
third probable answer
{
this->option3=str;
}
string get_option3()//method to access the third
probable answer
{
return
this->option3;
}
void set_option4(string str)//method used to set
fourth probable answer
{
this->option4=str;
}
string get_option4()//method to access the
fourth probable answer
{
return
this->option4;
}
void set_answer(string n)//method used to set
correct answer option
{
this->answer=n;
}
string get_answer()//method to access the
correct answer option
{
return
this->answer;
}
};
int main()
{
Question obj[10];//creating array of 10 objects
of class Question
ifstream inFile;//to read the file
string x;
int player1=0;//to store the points of
player1
int player2=0;//to store the points of
player2
inFile.open("trivia.txt");
for(int i=0;i<10;i++)//setting the data
members of each object of the array
{
getline(inFile,x);//reading from file the trivia question
obj[i].set_trivia(x);
getline(inFile,x);
obj[i].set_trivia(x);
getline(inFile,x);
obj[i].set_option1(x);
getline(inFile,x);
obj[i].set_option2(x);
getline(inFile,x);
obj[i].set_option3(x);
getline(inFile,x);
obj[i].set_option4(x);
getline(inFile,x);
obj[i].set_answer(x);
}
for(int i=0;i<10;i++)//loop to start the
game
{
if(i%2==0)// for first
player
{
cout<< "Player 1's turn"<<endl;
cout<<obj[i].get_trivia()<<endl;
cout<<obj[i].get_option1()<<endl;
cout<<obj[i].get_option2()<<endl;
cout<<obj[i].get_option3()<<endl;
cout<<obj[i].get_option4()<<endl;
//cout<<obj[i].get_answer()<<endl;
cout<<"submit your answer"<<endl;
string str;// asking the player for answer
cin >> str;
//compare user's input with correct answer if he gave correct
answer
//then add 1 to his points tally
if(str.compare(obj[i].get_answer()) == 0)
{
player1++;
}
}
else //for second
player
{
cout<< "Player 2's turn"<<endl;
cout<<obj[i].get_trivia()<<endl;
cout<<obj[i].get_option1()<<endl;
cout<<obj[i].get_option2()<<endl;
cout<<obj[i].get_option3()<<endl;
cout<<obj[i].get_option4()<<endl;
//cout<<obj[i].get_answer()<<endl;
cout<<"submit your answer"<<endl;
string str;
cin >> str;
//compare user's input with correct answer if he gave correct
answer
//then add 1 to his points tally
if(str.compare(obj[i].get_answer()) == 0)
{
player2++;
}
}
}
//display the points of each player
cout<<"points of Player1 is
"<<player1<<endl;
cout<<"points of Player2 is
"<<player2<<endl;
//compare points to determine the winner
if(player1<player2)
{
cout<<"Player2 is
the winner "<<endl;
}
else
{
cout<<"Player1 is
the winner "<<endl;
}
inFile.close();
return 0;
}
Screenshot of code as well as Output is given below:





OUTPUT:



If you have any doubt about the above solution, feel free to ask them through comments.
If you find this piece of information helpful then do give it a thumbs up.
Happy to help :)
the main problem i have with this project is getting the "trivia.txt" file to be printed...
In this programming exercise, you will create a simple trivia game for two players. The program will work like this: Starting out with player 1, each player gets a turn at answering 5 trivia questions. (There should be a total of 10 questions.) When a question is displayed, 4 possible answers are also displayed. Only one of the answers is correct, and if the player selects the correct answer, he or she earns a point. After answers have been selected...
java In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow for managing the question bank. Question bank management will include adding new questions, deleting questions and displaying all of the questions, answers and point values. 2. The project can be a GUI or a menu based command line program. 3. Project details 1. Create a class to...
java In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow for managing the question bank. Question bank management will include adding new questions, deleting questions and displaying all of the questions, answers and point values. 2. The project can be a GUI or a menu based command line program. 3. Project details 1. Create a class to...
java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...
Programming Assignment Objective Write a Java program that utilizes multiple classes. Write a Java program that utilizes inheritance in a practical manner. Problem: Quiz Bowl Your high school quiz bowl team has been losing its edge and needs to find a method to improve. Knowing that you are a savvy programmer, your coach asks you to write a program that the team members can use to hone their skills. Quiz Bowl questions come in three varieties: True/False Multiple Choice (variable...
I am suppose to have my array before the main class but I am getting the error 7 errors found: File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 60] Error: non-static variable objectArray cannot be referenced from a static context File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 61] Error: non-static variable objectArray cannot be referenced from a static context File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 67] Error: non-static variable objectArray cannot...
You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: description - which is a string write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money - double 3. number of questions that must be answered to win - integer. 4. write the accessor, mutator, constructor,...
A File class object may refer to a data file or a directory. The IntelliJ project Directory List Demo, discussed starting on page 8, shows how to get information about the contents of a directory using File class methods list() and listFle(). The IntelliJ project Create DirectoriesDemo, starting on page 10, shows how to make a set of directories using the File class method mkdir(). The IntelliJ project CopyFileDemoE has a method to copy a file. Your task is to...
Haloo , i have java program , Java Program , dynamic program Given a knapsack with capacity B∈N and -n- objects with profits p0, ..., p n-1 and weights w0, ..., wn-1. It is also necessary to find a subset I ⊆ {0, ..., n-1} such that the profit of the selected objects is maximized without exceeding the capacity. However, we have another limitation: the number of objects must not exceed a given k ∈ N Example: For the items...
Hello I need help with python programming here is my code # Trivia Challenge # Trivia game that reads a plain text file import sys def open_file(file_name, mode): """Open a file.""" try: the_file = open(file_name, mode) except IOError as e: print("Unable to open the file", file_name, "Ending program.\n", e) input("\n\nPress the enter key to exit.") sys.exit() else: return the_file def next_line(the_file): """Return next line from the trivia file, formatted.""" line = the_file.readline() line = line.replace("/", "\n") return line def next_block(the_file):...