Quiz/Trivia Game Using Classes! Help needed!

Right down to the nitty-grity. I'm enrolled in a C++ course and we have a group project due Wednesday. We've been instructed to create a quiz show game that reads in 25 questions from a file and outputs them from an array. We need 2 classes, one called question that holds and displays questions and answers using string variables, and a Quiz class that manages the questions array and tallies up correct/incorrect answers. Besides these 2 header files, we also need a main .cpp to run it, obviously.

Please scope these UMLs! http://img716.imageshack.us/img716/9517/umlp.png

I'm not asking you guys to write the code, but some general advice/direction would be awesome. We're struggling a bit to figure out how to integrate everything.
First things first, make the 'Question' classes that is capable of storing the questions and answers as required.

The UML diagram is pretty detailed as to how this class should be set out.

Once you've done that, construct the 'Quiz' class. Again, the UML leaves little to the imagination, so there shouldn't be too much trouble in constructing the actual class itself.

When you've got your classes set up, that's half of the work done.
so what would the code be?
I'm not going to write it.
Sooo... I got the brunt of it handled, but still having a few compile errors show their ugly heads in the cpp file. Here's the code as it stands:

CPP Running 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
#include <iostream>
#include "Quiz.h"
#include <iomanip>
#include <string>
#include <fstream>

int main()
{
	string question;
	string answer; 
	Question firstQuestion[5];
	ifstream inFile;

	string inFile.open("QuizShow.txt");

if (inFile.fail()) 
{ 
	cout << "Questions and answers could not be opened." << endl;
}
else
{
	int n = 0;
	while (!inFile.eof()) 
	{
		getline (inFile, question);
		getline (inFile, answer);

		firstQuestion[n] = Question(question, answer);
		quizOne.add(firstQuestion[n]);
		n++;
	}
}

Quiz::giveQuiz;

cout << "Final scores:" << endl << "Correct:" << Quiz::getNumCorrect << endl << "Incorrect:" << Quiz::getNumIncorrect << endl << "Thanks for playing!";

system ("pause"); 
return 0;

} 


Quiz.h
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;


class Question
{

private:
   string question;
   string answer;
	           
public:
   Question();
   Question (string query, string result);
   
   string getQuestion();
   string getAnswer();
   bool answerCorrect(string candidateAnswer);
   void displayQuestion();
};


Question::Question()
{
	question = "Unknown";
	answer = "Unknown";
}

Question::Question(string query, string result)
{
	question = query;
	answer = result;
}

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

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

bool Question::answerCorrect(string candidateAnswer)
{
	if (answer == question)
	return true;
	else
	return false;
}

void Question::displayQuestion()
{
	cout << "Question:"  << question << endl;
	cout << "Answer:" << answer << endl;
}


Question.h
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;


class Question
{

private:
   string question;
   string answer;
	           
public:
   Question();
   Question (string query, string result);
   
   string getQuestion();
   string getAnswer();
   bool answerCorrect(string candidateAnswer);
   void displayQuestion();
};


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

Question::Question(string query, string result)
{
	question = query;
	answer = result;
}

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

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

bool Question::answerCorrect(string candidateAnswer)
{
	if (answer == question)
	return true;
	else
	return false;
}

void displayQuestion()
{
	cout << "Question:"  << question << endl << "Answer:" << answer; 
}


And the errors I'm receiving:

1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\quizshow.cpp(34): error C2653: 'Quiz' : is not a class or namespace name
1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\quizshow.cpp(34): error C2065: 'giveQuiz' : undeclared identifier
1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\quizshow.cpp(36): error C2653: 'Quiz' : is not a class or namespace name
1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\quizshow.cpp(36): error C2065: 'getNumCorrect' : undeclared identifier
1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\quizshow.cpp(36): error C2653: 'Quiz' : is not a class or namespace name
1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\quizshow.cpp(36): error C2065: 'getNumIncorrect' : undeclared identifier



I know this is a simple fix, but my partner and I can't seem to figure it out.

Thanks guys!
You... never declared Quiz anywhere. You declared the class Question, but never Quiz. Was that what Quiz.h was supposed to do?

Also, before the compiler says anything about it, you also didn't declare quizOne. :/

EDIT: One last thing. You probably need some parentheses after those function names, assuming they're static functions. If they're not static functions... well, then there are some more changes that need to be made. :)

-Albatross
Last edited on
Oh snap. That was a mess-up on my end:

this is Quiz.h

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
#include <iostream>
#include "Question.h"
using namespace std; 

const int MAX_QUESTIONS = 25; 

class Quiz
{ 

private:
	Question questions[MAX_QUESTIONS];
	int current; 
	int correct;
	int incorrect;

public:
	Quiz();
	void add(Question newQuestion);
	void giveQuiz(); 
	int getNumCorrect();
	int getNumIncorrect();
};

	Quiz::Quiz()
	{
		current = 0;
		correct = 0;
		incorrect = 0;
	};

	void Quiz::giveQuiz()
	{ 
		string userInput;
		bool cont = false;
	for (int i = 0; i < MAX_QUESTIONS && !cont; i++)
	{ 
		if (questions[i + 1].getQuestion() == "Unknown") {cont = true; }
		cout << "Question [" << i + 1 << "]:" << questions[i].getQuestion() << endl;
		cout << "Answer: ";
		getline(cin, userInput);
		cout << endl; 
		if(operator == (questions[i].getAnswer(),userInput)) 
		{correct++;}
		else
		{incorrect++;}
	}

	void Quiz::add() 
	{
		questions[current]=newQuestion;
	}

	int Quiz::getNumCorrect()
	{
		return correct;
	}
	
	int Quiz::getNumIncorrect()
	{ 
		return incorrect;
	} 



EDIT: And new errors!
1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\question.h(62): error C2065: 'question' : undeclared identifier
1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\question.h(62): error C2065: 'answer' : undeclared identifier
1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\quiz.h(49): error C2601: 'Quiz::add' : local function definitions are illegal
1> c:\users\tommy\documents\visual studio 2010\projects\quizshow\quiz.h(32): this line contains a '{' which has not yet been matched
1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\quiz.h(54): error C2601: 'Quiz::getNumCorrect' : local function definitions are illegal
1> c:\users\tommy\documents\visual studio 2010\projects\quizshow\quiz.h(32): this line contains a '{' which has not yet been matched
1>c:\users\tommy\documents\visual studio 2010\projects\quizshow\quiz.h(59): error C2601: 'Quiz::getNumIncorrect' : local function definitions are illegal
1> c:\users\tommy\documents\visual studio 2010\projects\quizshow\quiz.h(32): this line contains a '{' which has not yet been matched
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(11): error C2870: 'std' : a namespace definition must appear either at file scope or immediately within another namespace definition
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(44): fatal error C1903: unable to recover from previous error(s); stopping compilation


I have no idea what's going on with fstream.
Last edited on
...odd. Some of the line numbers of the errors don't line up with the errors in your files. For instance, there isn't a line 62 in your question.h. :/

However! You are missing a } somewhere in your Quiz.h. Also, on line 48, you may want to compare what's there with what's on line 18. :)

Good luck!

-Albatross
Last edited on
Topic archived. No new replies allowed.