Whats wrong with my code??

Associate
Joined
6 Mar 2009
Posts
495
I am trying to create a quiz in C++ and here is what i have came up with so far:
#include <iostream>

using namespace std;

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

void 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 press a number to correspond your answer:" << endl;

}//displayQuestion

void correctAnswer(int usersInput, int usersScore)
{
if(usersInput ==1)
{
usersScore+2;
}

}//correctAnswer
};

void getUsersAnswer(int usersInput)
{
cin >> usersInput;

}//getUsersAnswer


int main()
{

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

//creating question object
Question q;

//calling displayQuestion function
q.displayQuestion();

//method to get users input
getUsersAnswer(usersInput);

// setting usersScore to zero
q.usersScore=0;

// calling usersScore function
q.correctAnswer(usersInput, usersScore);

system ("pause");
return 0;
}

I am getting errors when calling the usersScore and usersInput functions:(

Can someone give me some guidance please.
 
I think he means on your instantiation of the Question, but you can use either anyway.

Using the new keyword (Question* q = new Question()) adds the object to the heap and returns a pointer, the alternative (Question q) adds it to the stack and returns the object.

I don't use C++ so I can't really help much, but I don't see any userInput var declaration anywhere, just the method parameters, perhaps that's another C++ feature or it might be of some use.
 
Last edited:
I had made a typing error in that code that said usersInput instead of usersAnswer, but i changed it and still getting an error saying that the variables are undefined.
 
I suspect it's the getUsersAnswer(usersInput); part then. That method doesn't make much sense to me.

Code:
void getUsersAnswer(int usersInput)
{
cin >> usersInput;
}

You're passing in a parameter called usersInput that isn't defined anywhere, I presume to get the users answer, which cin >> usersInput/usersAnswer does. I'd just lump it all into one AskQuestion function that returns the users score.

The code you pasted isn't very clear, it looks like you're defining the getUsersAnswer method outside of the Question class and there's a few variables that aren't used anywhere.

I'd think about whose responsibility it is to track the score first. You have a users score variable defined inside the question but a question doesn't need to know what the users score is, it just has a question and an answer. I'd keep track of the score inside the main method, that way you can append scores (either 0 if the answer is wrong, or (it looks like) 2 if they're correct).

I won't write the code but here is how I'd do it in psuedo code. It really depends how you want to display the possible answers, you could store them in an array and return them when the question is displayed, or just cout them like you are.

Code:
class Question
{
    string question = "what's the answer?";
    string possibleAnswers[] = define them in a constructor;
    int answer = 3;

    int AskQuestion()
    {
        int usersAnswer;

        // ask the question
        cout >> question;

        // display the possible answers
        foreach(answer in possibleAnswers)
            cout >> answer;

        // ask for their answer
        cout << "enter the number of the answer";

        // get their answer
        cin >> usersAnswer;

        // if their answer was the same as the actual answer (they were right!) give them 2 points
        if(usersAnswer == answer)
             return 2;
        
        // otherwise give them the actual answer
        cout >> "wrong, the answer was: " + answer;

        return 0;
    }
};

Obviously you don't want to hard code the question and answers into the Question class though, otherwise you have to create multiple Question classes, which defeats the point of OO. You want to have a constructor that takes in three values - the question, an array of the possible answers, and the actual answer.

I'd then display the actual quiz like this

Code:
int main()
{
    int score = 0;
    Question[] questions; // an array of all the questions, you could just do Question q; for one question

    cout << "welcome to the quiz";

    foreach(question in questions)
    { 
         // ask the question and add the score from the question to the overall score
         int questionScore = question.AskQuestion();
         score = score +  questionScore;
    }

    system ("pause");
    return 0;
}
 
Last edited:
Thanks for the reply. I understand the way you are setting it out but unfortunately i have been given a set way for how it should be set out but it seems harder to me.

It should have a class Question which should have methods to:

Display the questions
Obtain answers for user
Return the users score
Display comments on there answers

Then there should be another class called Quiz which should:

Present the questions in order
Display the total score.

Its the second class called Quiz that i dont understand because isnt the first class doing the exact same thing??
 
Back
Top Bottom