Trivia Game using classes

I am working on an intro C++ project and need a little help with my code. I am new to learning classes and am struggling a bit with getting them to work. I am making a trivia game that will use two classes: "Test" which will take care of the questions array and count up correct and wrong answers, and "Questions" which will display the questions and answers using strings! I have written an input file, Questions.h, Test.h and main.cpp

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
#include <iostream>
#include "Test.h"
#include "Questions.h"
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    string question;
    string answer;
    Questions question_1[5];
    ifstream inputFile;

    string inputFile.open("TestQuestions.txt");

//lets the player know that the file opening was unsuccessful
if (inputFile.fail())
{
    cout << "File could not be opened" << endl;
}

else
{
    int n = 0;
    while(stream >> var)
    {
        getline(inputFile, question);
        getline(inputFile, answer);

        question_1[n] = Questions(question, answer);
        n++;
    }
}
//display the total correct answers
Test::giveTest;
cout << "Here are the final scores..." << endl << "Correct Answers: " << Test.getCorrectCount() << endl << "Great job!  Thanks for playing.";

return 0;

}
Last edited on
Here is one of my classes
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 <iomanip>
#include <cstdlib>
using namespace std;
//defining a new class
class Questions
{
private:
    string question;
    string answer;

public:
    Questions()
    Questions(string prompt, string result);
    string getQuestion();
    string getAnswer();
    bool correctAnswer(string answerGuess);
    void showQuestion();
};

Questions::Questions()
{
    question = "unknown";
    answer = "unknown";
}

Questions::Questions(string prompt, string result);
{
    question = prompt;
    answer = result;
}

string Questions::getQuestion()
{
    return question;
}

string Questions::getAnswer()
{
    return answer;
}

bool Questions::correctAnswer(string answerGuess)
{
    if (answer == answerGuess)
    return true;
    else
        return false;
}

void Questions::showQuestion()
{
    cout << "Question: " << endl << "Answer: ";
}

Last edited on
First post
Line 27: Do not loop on ! stream.eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (stream >> var) 
  {  //  Good operation
  }


Line 33: Questions does not have a function named add. In fact, you do not even need this line.

Line 38: If this is supposed to be a function call, you need ().

Line 39: Test::getCorrectCount needs ().

Line 38,39: You need to reference an instance of Test, not the class name.

Second post:
1
2
3
bool Questions::correctAnswer(string answerGuess)
{
if (answer == question)

Do you really want to compare the question and the answer? Shouldn't you be comparing answer and answerGuess?

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Last edited on
Thank you very much, that was helpful! I just made those changes. Here is my other class that corresponds to the program...still a little glitchy

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
#include <iostream>
#include "Questions.h"
using namespace std;
//max number of questions possible is initialized at 5 here
const int numberQuestions = 5;
//defining a new class
class Test
{
private:
    Questions asking[numberQuestions];
    int current;
    int correct;
    int wrong;

public:
    Test();
    void add(Questions newQuestion);
    void giveTest();
    int getCorrectCount();
    int getWrongCount();
};
//start all the amounts at zero
Test::Test()
{
    current = 0;
    correct = 0;
    wrong = 0;
};

void Test:: giveTest()
{
    string playerInput;
    bool content =  false;
    for(int i = 0; i < numberQuestions && !content; i++)
    {
        if (asking[i + 1].getQuestion() == "Unknown") {content = true;}
        cout << "Question [ " << i + 1 << "]: " << asking.getQuestion() << endl;
        cout << "Answer: ";
        getline(cin (playerInput));
        cout << endl;
        if(operator == (asking[i].getAnswer(), playerInput))
        {
            correct ++;
        }
        else
        {
            wrong ++;
        }
    }

}

void Test::add()
{
    asking[current] = newQuestion;
}

void Test::getCorrectCount()
{
    return correct;
}

void Test::getWrongCount()
{
    return wrong;
}
Last edited on
Your code tags need work.

They should be
[code]
your code here
[/code]

Please edit your posts.

Questions.cpp
line 14: Remove the ; from your constructor.

Line 19: The function declaration for getQuestion() is commented out.

Line 23: Ditto for getAnswer().

Line 34: Ditto for showQuestion().

Line 34: showQuestion should be qualified by the class name.

test.cpp
Line 15: asking[i + 1].getQuestion() is going to cause an out of bounds reference. Members of the array are 0-4. You're trying to reference [5] on the last iteration.

Line 17: asking.getQuestion() asking needs a subscript.

Line 21: if(operator == (asking[i].getAnswer(), playerInput))
That's an odd way to do the comparision. Consider:
 
  if (asking[i].getAnswer() == playerInput)


line 21: getline(cin (playerInput)); You need a , between cin and playerinput.

line 30: void Test::add() Does not match the function declaration.

Line 31: You add a question, but you don't adjust current.

Line 34: void Test::getCorrectCount() You're trying to return a value from a void function. Also, does not match the function declaration.

Line 38: Ditto for getWrongCount().

main.cpp
Line 13: Don't declare questions here. They're part of Test.

Line 16: string does not belong in front of your open.

Line 27: stream and var are undefined. My use of stream and var in my previous post about eof was only an example. What you want is:
1
2
 
  while (getline(inputFile, question) && getline(inputFile, answer))


Line 32: You should be adding your Question object to Test (which you haven't declared).

Line 37: Again, you need to instantiate Test and make a proper function call.
1
2
3
4
 
  Test test; 
...
  test.giveTest();


Line 39: Test.getCorrectCount() Test is a class name, not an instance of the class.

Note: My line numbers are approximate since your code tags were messed up and I had to guess.




Last edited on
Topic archived. No new replies allowed.