Trivia game program[Need help asap,3 hours til due]

Hello everyone, I am trying to make a program that allows 2 players to take turns answering multiple choice questions.Each time a player gets a answer right they get a point and whoever has the most points win. Below i have my header file but i am not sure of how to right my source code. Could someone show me an example of how to this up? Thank you in advance, yall are great help to those who are just learning to program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef QUESTION_H
#define QUESTION_H
using namespace std;

#include <string>

class Question
{
private:
	string triviaQuestion;
	string answerOne;
	string answerTwo;
	string answerThree;
	string answerFour;
	int correctAnswer;

public:
	Question();
	Question(string question, string one, string two, string three,
		string four, int answer);

	void setTriviaQuestion(string question = "");
	void setAnswerOne(string one ="");
	void setAnswerTwo(string two="");
	void setAnswerThree(string three="");
	void setAnswerFour(string four="");
	void setCorrectAnswer(int answer=0);

	string getTriviaQuestion();
	string getAnswerOne();
	string getAnswerTwo();
	string getAnswerThree();
	string getAnswerFour();
	int getCorrectAnswer();

	bool checkGivenAnswer(int chosenNumber);

};


#endif 
Last edited on
Your declarations seems good (you don't have to provide identifiers of variables in the function declarations, though). Also you should use an array for the answers and the functions to access them with the index as a parameter (assuming there is always 4 answers). I take it you are not sure how to implement this?

Implementation in .cpp file (I have omitted error checking for the constructors/setters; you can do that yourself).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Question::Question() { }

Question::Question(string question, string one, string two, string three, string four, int answer) :
triviaQuestion(question), answerOne(one), answerTwo(two), answerThree(three), answerFour(four), correctAnswer(answer) { }

void Question::setTriviaQuestion(string question) { triviaQuestion = question; )
void Question::setAnswerOne(string one) { answerOne = one; };
void Question::setAnswerTwo(string two) { answerTwo = two; }
void Question::setAnswerThree(string three) { answerThree = three; }
void Question::setAnswerFour(string four) { answerFour = four; }
void Question::setCorrectAnswer(int answer) { correctAnswer = answer; }

string Question::getTriviaQuestion() { return triviaQuestion; }
string Question::getAnswerOne() { return answerOne; }
string Question::getAnswerTwo() { return answerTwo; }
string Question::getAnswerThree() { return answerThree; }
string Question::getAnswerFour() { return answerFour; }
int Question::getCorrectAnswer() { return correctAnswer; }

bool Question::checkGivienAnswer(int chosenNumber) { return correctAnswer == chosenNumber; }
For the program I am supposed to get the questions , answers, and correct answer from a text file "trivia.txt", how can one do this?
You'll need to work with fstream for the that. Look it up, I'm sure you'll find something handy. It gives you the ability to read and write to text files.
ok ive read through the tutorials and i know how to get a single line or a certain section at a time but can someone show me an example/link me an example of how to get a certain section for a string variable? What im trying to do is after the first line is a question followed by the next 4 lines being answer choices and then a correct answer line , then it repeats 10 times . I need to input that text into the setVariable functions above.
Well I think what you're missing is just a for loop if you've understood that. Then you'd pass it through to your function via ex. setAnswerOne(variable that you stored the line of this information to in your text document). If you read text from a text document using fstream just store it to a string type and pass that to your function. Trying to elaborate the best that I can here so I hope that helped.
here is the code i have so far im just trying to get the question part working first. Line 22.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//Question.cpp
#include <iostream>
#include <string>
#include "Question.h"
#include <fstream>

Question::Question()
{
	setTriviaQuestion();
	setAnswerOne();
	setAnswerTwo();
	setAnswerThree();
	setAnswerFour();
	
}

Question::Question(string question, string one, string two, string three,
	string four, int answer) : triviaQuestion {question}, answerOne {one}, answerTwo {two},
	answerThree {three}, answerFour {four}, correctAnswer {answer}
{}

void Question::setTriviaQuestion(string question)
{
	ifstream infile;
	infile.open("trivia.txt");
	getline(infile, question);
	infile.close();

	triviaQuestion = question;
}

void Question::setAnswerOne(string one)
{
	answerOne = one;
}

void Question::setAnswerTwo(string two)
{
	answerTwo = two;
}

void Question::setAnswerThree(string three)
{
	answerThree = three;
}

void Question::setAnswerFour(string four)
{
	answerFour = four;
}

void Question::setCorrectAnswer(int answer)
{
	correctAnswer = answer;
}

string Question :: getTriviaQuestion()
{
	return triviaQuestion;
}
string Question::getAnswerOne()
{
	return answerOne;
}
string Question::getAnswerTwo()
{
	return answerTwo;
}

string Question::getAnswerThree()
{
	return answerThree;
}
string Question::getAnswerFour()
{
	return answerFour;
}
int Question::getCorrectAnswer()
{
	return correctAnswer;
}

bool Question::checkGivenAnswer(int choosenNumber)
{
	return (correctAnswer == choosenNumber);

}
//Lab7.cpp
#include <iostream>
#include <string>
#include "Question.h"

using namespace std;

int main()
{
	const int trivia = 10;
	int playerOne = 0;
	int playerTwo = 0;
	int playerOneScore = 0;
	int playerTwoScore = 0;

	Question triviaGame[trivia];

	for (int i = 0; i < trivia/2; i++)
	{
		
		int choosenNumber = 0;
		cout << "Player 1:" << endl;
		cout << triviaGame[i].getTriviaQuestion() << endl;
		cout << triviaGame[i].getAnswerOne() << endl;
		cout << triviaGame[i].getAnswerTwo() << endl;
		cout << triviaGame[i].getAnswerThree() << endl;
		cout << triviaGame[i].getAnswerFour() << endl;
		cout << "Choose an answer by entering a number 1 to 4: ";
		cin >> choosenNumber;
		if (triviaGame[i].checkGivenAnswer(choosenNumber) == true)
		{
			playerOneScore++;
		};
		
	}

	for (int i = 5; i < trivia; i++)
	{
		int choosenNumber = 0;
		cout << "Player 2:" << endl;
		cout << triviaGame[i].getTriviaQuestion() << endl;
		cout << triviaGame[i].getAnswerOne() << endl;
		cout << triviaGame[i].getAnswerTwo() << endl;
		cout << triviaGame[i].getAnswerThree() << endl;
		cout << triviaGame[i].getAnswerFour() << endl;
		cout << "Choose an answer by entering a number 1 to 4: ";
		cin >> choosenNumber;
		if (triviaGame[i].checkGivenAnswer(choosenNumber) == true)
		{
			playerTwoScore++;
		};
		
	}
	
	cout << "Player 1 Scored : " << playerOneScore << endl;
	cout << "Player 2 Scored : " << playerTwoScore << endl;

	if (playerOneScore > playerTwoScore)
	{
		cout << "The winner is Player 1" << endl;
	}
	else if (playerOneScore < playerTwoScore)
	{
		cout << "The winnder is Player Two" << endl;
	}
	else if (playerOneScore == playerTwoScore)
	{
		cout << "The score is tied, no winner" << endl;
	}
	return 0;
}
//trivia.txt
What is Pi?
3.14
2.72
0
A Food
1
What is e?
3.14
2.72
0
A food
2
What is a cpu?
The Central Processing Unit
A brand of computer
The antivirus software
The memory of a computer.
1
What is the natural log of e?
1
13
3
1.826
1
What is the 1kg in pounds?
2.2
3.2
1.2
1
1
What is the speed of light?
200653789 m/s
8026453 m/s
100000000 m/s
299792458 m/s
4
What is the speed of sound?
502465 m/s
1200 m/s
340 m/s
100 m/s
3 
What is the chemical formula for Diamond?
C
D
au
pg
2
What is the weight of the earth in tons?
6.39 x 10^23 
5.98 x 10^21 
8.47 x 10^22
1.04 x 10^22
2
What is the slowest color?
Viloet 
Red
Blue
Green
1
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <fstream>

struct question
{
    std::string text ;

    static constexpr std::size_t N = 4 ;
    std::string answers[N] ; // the answers in an array of N string

    std::size_t num_correct_answer ;

    bool correct( std::size_t num_ans ) const { return num_ans == num_correct_answer ; }

    // read a question from an input stream
    friend std::istream& operator>> ( std::istream& stm, question& q )
    {
        // throw away leading new line if present, and skip leading empty lines
        do std::getline( stm, q.text ) ; while( q.text.empty() && stm.good() ) ;

        for( std::string& ans : q.answers ) std::getline( stm, ans ) ;

        return stm >> q.num_correct_answer ; // add a check if it is within [1,N]?
    }

    // print the question to an output stream
    // (note: designed for printing to stdout, so not symmetric with >> )
    friend std::ostream& operator<< ( std::ostream& stm, const question& q )
    {
        stm << q.text << '\n' ;

        for( std::size_t i = 0 ; i < N ; ++i )
            std::cout << i+1 << ". " << q.answers[i] << '\n' ;

        return stm ;
    }
};

int main()
{
    std::ifstream file( "trivia.txt" ) ;

    question q ;
    while( file >> q )
    {
        std::cout << "\n\n" << q << "\nanswer [1-" << question::N << "]? " ;
        int ans = 0 ;
        std::cin >> ans ; // assumes that user enters an integer;
                          // input error handling elided for brevity

        if( q.correct(ans) ) std::cout << "correct\n" ;
        else std::cout << "wrong\n" ;
    }
}

Take it up from there.
Without overloaded operators (if that hasn't been taught as yet):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <fstream>

struct question
{
    std::string text ;

    static constexpr std::size_t N = 4 ;
    std::string answers[N] ; // the answers in an array of N string

    std::size_t num_correct_answer ;

    bool correct( std::size_t num_ans ) const { return num_ans == num_correct_answer ; }

    // read a question from an input stream
    bool read_from( std::istream& stm )
    {
        // throw away leading new line if present, and skip leading empty lines
        do std::getline( stm, text ) ; while( text.empty() && stm.good() ) ;

        for( std::string& ans : answers ) std::getline( stm, ans ) ;
        
        // return true if read was successful 
        return bool( stm >> num_correct_answer ) ; // add a check if it is within [1,N]?
    }

    // display the question on an output stream
    void display( std::ostream& stm = std::cout )
    {
        stm << text << '\n' ;

        for( std::size_t i = 0 ; i < N ; ++i )
            std::cout << i+1 << ". " << answers[i] << '\n' ;
    }
};

int main()
{
    std::ifstream file( "trivia.txt" ) ;

    question q ;
    while( q.read_from(file) )
    {
        std::cout << "\n\n" ;
        q.display() ;

        std::cout << "\nanswer [1-" << question::N << "]? " ;
        int ans = 0 ;
        std::cin >> ans ; // assumes that user enters an integer;
                          // input error handling elided for brevity

        if( q.correct(ans) ) std::cout << "correct\n" ;
        else std::cout << "wrong\n" ;
    }
}
i have no idea what friend and operator , we haven't covered those yet.
> i have no idea what friend and operator , we haven't covered those yet.

You may have missed this post: http://www.cplusplus.com/forum/beginner/214498/#msg998688
im not allowed to use struct , it has to be class orientated.
ok for this, how do i get it to skip down 6 lines for each array object. Im trying to get it to return line1 for object1 then line 7 for object 2, etc.right now it just gets the first line.

1
2
3
4
5
6
7
8
9
10
void Question::setTriviaQuestion(string question)
{
	
		ifstream infile;
		infile.open("trivia.txt");
		getline(infile, question);
		infile.close();

		triviaQuestion = question;
}
The class keys struct and class are indistinguishable in C++, except that the default access mode and default inheritance mode are public if class declaration uses the struct class-key and private if the class declaration uses the class class-key. Both class and struct can be used in a class definition.
http://en.cppreference.com/w/cpp/language/classes


Using the keyword class (and with access control added) the class-type would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class question
{
    public:
        static constexpr std::size_t N = 4 ; // number of possible answers

    private:

        std::string text ;

        std::string answers[N] ; // the answers in an array of N string

        std::size_t num_correct_answer = 1 ;

    public:

        bool correct( std::size_t num_ans ) const { return num_ans == num_correct_answer ; }

        // read a question from an input stream
        bool read_from( std::istream& stm )
        {
            // throw away leading new line if present, and skip leading empty lines
            do std::getline( stm, text ) ; while( text.empty() && stm.good() ) ;

            for( std::string& ans : answers ) std::getline( stm, ans ) ;

            return bool( stm >> num_correct_answer ) ; // add a check if it is within [1,N]?
        }

        // display the question on an output stream
        void display( std::ostream& stm = std::cout )
        {
            stm << text << '\n' ;

            for( std::size_t i = 0 ; i < N ; ++i )
                std::cout << i+1 << ". " << answers[i] << '\n' ;
        }
};
Last edited on
we have to use the header file i posted at the very top. this is cutting all of that out if im understanding it.
OP: you're quite insistent on having things exactly your way while you have been provided three comprehensive solutions that cover all that you need. Why don't you go away, do some work on your own and try and fit what you have been sent according to your requirements?
Topic archived. No new replies allowed.