Ok, so as suggested I have rewritten this thread.

It was suggested that the code posted to be shortened and only revelent code be posted. I did both of these and people started asking where all different things were, and why cant they see other files etc.

Uploaded is the full code to the revelent part, and compiling this will give you a stripped down version of a MUCH bigger program.

The code is compiled in Codeblocks.

The problem is with this part of the code:

RiddleGenerator::QnA RiddleGenerator::mQnA[RiddleGenerator::QuestionCount] = {
        { "What shape as 4 sides?\n\t1)Triangle\n\t2)Circle\n\t3)Square", 3 },
        { "What is the capital of England?\n\t1)Paris\n\t2)London\n\t3)Washington", 2 }

        // NEW QUESTIONs CAN GO IN THE ABOVE SPACE <---!
};

which is in Question_Math_Riddle_Generator.h line 166.

The main game loop is at the bottem of main.cpp as the top is all the connections between the rooms.

The question is:

Can the questions shown above be loaded from a text file and continue to function correctly. The purpose of this is so the person this application is given to, can edit the questions without having to have the source code.

The purpose of Question_Math_Riddle_Generator.h is to create random questions both riddle and math when called. Each time the user moves to a new room a question is called.

I know the problem is aorund the I/O ffstream area, I just DON'T KNOW how to adapt it for a struct.

Attachments
// The C++ Adventure Game Version 3.
// Year 12 I.T :: Mr Haughton :: 2009
// Darryl Mitchell
// Aliaks Inc. All Rights Reserved.


#ifndef QUESTION_MATH_RIDDLE_GENERATOR_H_INCLUDED
#define QUESTION_MATH_RIDDLE_GENERATOR_H_INCLUDED

#include <iostream>
#include <vector>
#include <string>
#include <time.h>
#include <stdio.h>
#ifdef WIN32
#include <Windows.h>
#define snprintf _snprintf
#endif

#define PROGRAMMING_ERROR std::cerr << "Programming error at " << __FILE__ << " line " << __LINE__ << std::endl;

using namespace std;

static int randBetween(int low, int high)
{
    if (low == high) return low;
    if (low > high)
    {
        int tmp = low;
        low = high;
        high = tmp;
    }

    static bool firstTime = true;
    if (firstTime)
    {
        srand((unsigned int)time(NULL));
        firstTime = false;
    }
    int num = rand();
    int result = num % (high - low + 1) + low;
    return result;
}

class QuestionGenerator
{
    /*
    Defines the public interface to question generators
    */
public:
    virtual const string& poseQuestion(void) = 0;
    virtual bool isCorrectAnswer(const std::string& answer) const = 0;
};

class ArithmeticQuestionGenerator: public QuestionGenerator
{
    /*
    Creates arithmetic question
    */
public:

    virtual const string& poseQuestion(void)
    {
        Operation operation = (Operation)randBetween(ADD, SUBT);

        // THIS IS WHERE YOU CHANGE THE NUMBERS.
        //
        // Operand1 = randBetween(A, B);
        // Operand2 = randBetween(C, D);
        //
        // A >= D
        // B >  A
        // C <= D
        // D >  D
        //
        // If you want positive and predictable outcomes.

        int operand1 = randBetween(0, 50);
        int operand2 = randBetween(0, 25);


        char buf[128];
        snprintf(buf, sizeof(buf), "What is %d %c %d ?",
            operand1, operationChar(operation), operand2);
        mCurrentQuestion = buf;

        switch(operation)
        {
        case ADD:
            mAnswer = operand1 + operand2;
            break;
        case SUBT:
            mAnswer = operand1 - operand2;
            break;
        default:
            PROGRAMMING_ERROR;
        }
        return mCurrentQuestion;
    }

    virtual bool isCorrectAnswer(const std::string& answer) const
    {
        return (atoi(answer.c_str()) == mAnswer);
    }

private:
    enum Operation
    {
        ADD = 1,
        SUBT
    };

    char operationChar(Operation op) {
        switch(op)
        {
        case ADD:
            return '+';
        case SUBT:
            return '-';
        default:
            PROGRAMMING_ERROR;
            break;
        }
        return ' ';
    }

    string mCurrentQuestion;
    double mAnswer;
};

class RiddleGenerator: public QuestionGenerator
{
public:
    /*
    Trivial pursuit questions
    */
    virtual const string& poseQuestion(void)
    {
        int q = randBetween(0, QuestionCount-1);
        mAnswer = mQnA[q].answer;
        return mQnA[q].question;
    }

    virtual bool isCorrectAnswer(const string& answer) const
    {
        return (atoi(answer.c_str()) == mAnswer);
    }

private:
    struct QnA {
        string question;
        int answer;
    };

    static const int QuestionCount = 2;
    static QnA mQnA[QuestionCount];
    int mAnswer;
};

// THIS IS WHERE YOU INSERT QUESTIONS.
//
// The structure MUST remain the same
//
// { "QUESTION?\n\t1)CHOICE1\n\t2)CHOICE2\n\t3)SO ON", The Number which relates to the ANSWER }

RiddleGenerator::QnA RiddleGenerator::mQnA[RiddleGenerator::QuestionCount] = {
        { "What shape as 4 sides?\n\t1)Triangle\n\t2)Circle\n\t3)Square", 3 },
        { "What is the capital of England?\n\t1)Paris\n\t2)London\n\t3)Washington", 2 }

        // NEW QUESTIONs CAN GO IN THE ABOVE SPACE <---!
};

class QuizMaster : public QuestionGenerator
{
    /*
    Responsible for holding different types of question generators
    */
public:
    QuizMaster()
    {
        mCurrentQuestionGenerator= NULL;
        mGenerators.push_back(new RiddleGenerator());
        mGenerators.push_back(new ArithmeticQuestionGenerator());
    }

    virtual const string& poseQuestion(void)
    {
        int ix = randBetween(0, int(mGenerators.size()-1));
        mCurrentQuestionGenerator = mGenerators[ix];
        return mCurrentQuestionGenerator->poseQuestion();
    }

    virtual bool isCorrectAnswer(const std::string& answer) const
    {
        if (mCurrentQuestionGenerator == NULL) return false;
        return mCurrentQuestionGenerator->isCorrectAnswer(answer);
    }

    vector<QuestionGenerator*> mGenerators;
    QuestionGenerator* mCurrentQuestionGenerator;
};

#endif // QUESTION_MATH_RIDDLE_GENERATOR_H_INCLUDED

Sorry I missed two files.

Attachments
// The C++ Adventure Game Version 3.
// Year 12 I.T :: Mr Haughton :: 2009
// Darryl Mitchell
// Aliaks Inc. All Rights Reserved.


#ifndef USER_H_INCLUDED
#define USER_H_INCLUDED

#include <Windows.h>

#include <string>
#include <iostream>
#include <vector>
#include <string>
#include <time.h>
#include <stdio.h>
#ifdef WIN32

#define snprintf _snprintf
#endif

#define PROGRAMMING_ERROR std::cerr << "Programming error at " << __FILE__ << " line " << __LINE__ << std::endl;


using namespace std;

class Room
{
public:
	string name;
	string description;

	Room *north;
	Room *south;
	Room *east;
	Room *west;

	Room (): north(),south(),east(),west() {}
};

class User
{
public:

    Room *location;

    void vSetUserName(string);        // Function for setting the username, takes a string
    void vSetAge(int);                // Function for setting the age, takes an int

    string sGetUserName() const;      // Function for retrieving the username
    int iGetAge() const;              // Function for retrieving the age

private:
    string m_sUsername;
    int m_iAge;
};

#endif // USER_H_INCLUDED
// The C++ Adventure Game Version 3.
// Year 12 I.T :: Mr Haughton :: 2009
// Darryl Mitchell
// Aliaks Inc. All Rights Reserved.


#include "classes.h"

void User::vSetUserName(std::string sUsername)
{
    m_sUsername = sUsername; // Store the contents of sUsername in m_sUsername
}

void User::vSetAge(int iAge)
{
    m_iAge = iAge; // Store the value of iAge in m_iAge
}

std::string User::sGetUserName() const
{
    return m_sUsername; // Return the variable to the program
}

int User::iGetAge() const
{
    return m_iAge; // Return the variable to the program
}

well if you want to store questions in a text file where the user can change them the i would suggest just writing a simple output protocol where it is outputted line by line like:

What shape as 4 sides?
1)Triangle
2)Circle
3)Square
3
make this line a blank line
start the next question here

then you could read the file line by line and put your formatting of the string in when you read it from the file. i believe this would very easy for a user to change the questions and answers. hopefully this makes sense.

Thank you Nathan :) That is very helpful, I hope this works for my structure :)

well if you want to store questions in a text file where the user can change them the i would suggest just writing a simple output protocol where it is outputted line by line like:

What shape as 4 sides?
1)Triangle
2)Circle
3)Square
3
make this line a blank line
start the next question here

then you could read the file line by line and put your formatting of the string in when you read it from the file. i believe this would very easy for a user to change the questions and answers. hopefully this makes sense.

This article has been dead for over six months. Start a new discussion instead.