Classes in C++

Associate
Joined
6 Mar 2009
Posts
495
I have to create a quiz program in C++. It should have two classes. The first class called 'question' should hold all data and methods for a single question and the second class called 'quiz' should hold an array of questions, obtain the users answer and display there score.

Im not sure if im on the right track or not, and not sure how to go about adding the questions and answers etc.

Here is what i have done so far:

Code:
class question
{
public:
	string question;
	string possAnswers;
	int marks;
	string comment;
	string usersAnswer;
	int usersScore;
	
	void displayQuestion(string question);
	void usersAnswer(string usersAnswer);
	void usersScore(int usersScore);
	void displayComments(string comment);

};

class quiz
{
	string q [];
	void totalScore();
	void displayComments();

};
 
Im having a problem when i go to run my program. Its says "Unable to start program, file cannot be specified".

But it is showing no errors in the code:(
 
ok i have got it to work now. Im having problems passing parameters from my class to the object of the class.
#include <iostream>

using namespace std;

class Question
{
public:
string question;
string possAnswers;
int marks;
string comment;
string usersAnswer;

void displayQuestion(string question,string usersAnswer)
{
cout << "Question 1" << endl;
cout << "How do you declare a variable x to be a pointer to a constant interger?" << endl;
cout << "1) int * const x" << endl;
cout << "2) int const * const x" << endl;
cout << "3) int const *x" << endl;
cout << "4) int *x" <<endl;

}

};


int main()
{
cout << "Welcome to my Quiz on C++. Please press 1-4 to answer each question.";

Question q;
q.displayQuestion(string question,string usersAnswer);

system ("pause");
return 0;
}

The problem is at- q.displayQuestion(string question,string usersAnswer).

Sorry im not to good at this:(
 
Im having problems Initializing an array inside my constructor.

class Question
{
public:

string question1, question2,question3,question4;
string answers1 [2];
string answers2 [4];
string answers3 [4];
string answers4 [4];

Question()
{
question1="Which of the statements in correct when using a header file:";
answers1= {"newClass","#include newClass"};

}

};

an error is appearing at answers1= part.

Any ideas??
 
Ok thanks:) Sorry for being a pain but i have a different piece of code from this thats getting errors.
class Question
{
public:
string question;
string possAnswers;
int marks;
string comment;
int usersAnswer;
int usersScore;


void displayQuestion();
void obtainAnswer();
void printUsersScore();
void displayQuestion2(int usersScore);
};


void Question::displayQuestion()
{

cout << endl;
cout << "Question 1" << endl;
cout << "How do you declare a variable x to be a pointer to a constant interger?" << endl;
cout << "1) int * const x" << endl;
cout << "2) int const * const x" << endl;
cout << "3) int const *x" << endl;
cout << "4) int *x" <<endl;
cout << "Please enter a number to correspond your answer:" << endl;

cin >> usersAnswer;

if(usersAnswer ==1)
usersScore +2;

else
usersScore +0;

}//displayQuestion

void Question::displayQuestion2(int usersScore)
{
cout << endl;
cout << "Question 2" << endl;
cout << "What is the statement used to print to the screen";
cout << "1) println" << endl;
cout << "2) cout" << endl;
cout << "3) cin" << endl;
cout << "System.out.print" << endl;
cout << "Please enter a number to correspond your answer:" << endl;

cin >> usersScore;

if(usersAnswer==2)
usersScore +2;

else
usersScore+0;

}//displayQuestion2

int main()
{
Question q;

cout << "Welcome to my Quiz on C++. Please press 1-4 to answer each question.";
cout <<endl;

q.displayQuestion();
q.displayQuestion2(usersScore);


system ("pause");
return 0;
}

What is the correct way to pass the usersScore into q.displayQuestion2();. As you can see i have done q.displayQuestion2(usersScore) but its getting errors:(
 
Back
Top Bottom