Triva Game

I'm attempting to write a program for a trivia game, but failing miserably. It's supposed to be a two player game, with player one answering questions 1-10 first and then player two answering the questions and the score being stored/displayed at the end. Honestly I'd just be happy if I could get help with the program returning what the correct answer is (which it's not).

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

class question
{
   private:
        string q[6];
        string ques;
        int correctnum;
   public:
      void setQues(string ques, string a1, string a2, string a3, string a4, string correct)
       {
         q[0] = ques;
         q[1] = a1;
         q[2] = a2;
         q[3] = a3;
         q[4] = a4;
         q[5] = correct;
       }
      void setCorrect(int correct);
      int getCorrect() const;

};

void question::setCorrect(int correctnum)
{
           q[5] = correctnum;
}


int question::getCorrect() const
{
        return correctnum;
};


int main ()
{

   ifstream qFile;
   qFile.open("trivia.txt");
   int playAns;
   question answer;
   string theString;
   int ans;
   for (int j = 0; j <= 6; j++)
   {
      getline(qFile, theString);
      if (j == 0)
          cout << "QUESTION: " << theString;
      else if (j < 5)
          cout << setw(5) << j << ". " << theString;
          cout << endl;
    }
      cout << "\nEnter your answer: ";
      cin >> playAns;

      cout<< "correct answer is: " << answer.getCorrect() << endl;

   return 0;
}

Last edited on
Hey, if you look at it your correctnum its never initialized anywhere. So you just getting some random number when you call the answer.getCorrect()

There are never actually anything called to initialize data members of question answer using setQues or setCorrect so all your string data members are initialized to default value ("" - empty string) and int has undefined value
Last edited on
Topic archived. No new replies allowed.