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...
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...
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...
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...
I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...
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...
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...
Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...
C program Read file problem: For example, I have an input.txt file like this: q 1 tq 4 p1 10 p2 5 p3 7 p4 20 p5 17 p6 9 p7 3 p8 11 p9 15 p10 1 I want the num of q is in one array, tq is in one array, the order of process(ie,p1,p2) is in one array, and process time is in one array(process time is the number after like p1,p2). There may many q,tq,p so...
Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...