while (true) {

		// Display the players location and possible movements.

		cout << "------------------" << endl;
		cout << "Location:   " << NewUser.location->name << endl;
		cout << "\nDescription:   " << NewUser.location->description << endl;
		if (NewUser.location->north)
			cout << "(N)orth to: " << NewUser.location->north->name << endl;
		if (NewUser.location->south)
			cout << "(S)outh to: " << NewUser.location->south->name << endl;
		if (NewUser.location->east)
			cout << "(E)ast to:  " << NewUser.location->east->name << endl;
		if (NewUser.location->west)
			cout << "(W)est to:  " << NewUser.location->west->name << endl;
		cout << "(Q)uit" << endl;

		// Get input and decide where to go next.

		char input;
		cin >> input;
		if (input == 'n' && NewUser.location->north)
			NewUser.location = NewUser.location->north;
		if (input == 's' && NewUser.location->south)

                cout << quizMaster.poseQuestion().c_str() << endl;
                getline(cin, answer);

            if (quizMaster.isCorrectAnswer(answer))
                cout << "Thats right" << endl;
            else
                cout << "Better luck next time !" << endl;

			NewUser.location = NewUser.location->south;

		if (input == 'e' && NewUser.location->east)
			NewUser.location = NewUser.location->east;
		if (input == 'w' && NewUser.location->west)
			NewUser.location = NewUser.location->west;
		if (input == 'q')
			break;
	}

	delete [] rooms;

Here is my loop.

Now if I put

cout << quizMaster.poseQuestion().c_str() << endl;
                getline(cin, answer);

            if (quizMaster.isCorrectAnswer(answer))
                cout << "Thats right" << endl;
            else
                cout << "Better luck next time !" << endl;

This in all of the other directions, it gets stuck saying better luck next time and then exe freezes and closes.

I have the full code which relates to the above as a attatchement.

Any Ideas?

Thanks in advance.

Attachments
#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);
        int operand1 = randBetween(5, 10);
        int operand2 = randBetween(0, 5);
        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;
};
RiddleGenerator::QnA RiddleGenerator::mQnA[RiddleGenerator::QuestionCount] = {
        { "What is your Quest?\n\t1)To seek the holy grail?\n\t2)Party Party Party", 1 },
        { "What is the best beer in Aussi Land?\n\t1)Tooheys\n\t2)Reschs\n\t3)Hahn", 1 }
};


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

It must mean that quizMaster.isCorrectAnswer(answer) is always returning 0 .Just check in that particular function for solving the problem. Its quite straight forward right.

Hey guys, sorry I have been quite busy and over the course of the week I have re worked my logic.

I now have

// Get input and decide where to go next.

		char input;
		cin >> input;

		if (input == 'q')
			break;

                cout << quizMaster.poseQuestion().c_str() << endl;
                getline(cin, answer);

		if (quizMaster.isCorrectAnswer(answer)) {
                	cout << "Thats right" << endl;

			if (input == 'n' && NewUser.location->north)
				NewUser.location = NewUser.location->north;
			if (input == 's' && NewUser.location->south)
				NewUser.location = NewUser.location->south;
			if (input == 'e' && NewUser.location->east)
				NewUser.location = NewUser.location->east;
			if (input == 'w' && NewUser.location->west)
				NewUser.location = NewUser.location->west;

		} else {
                	cout << "Better luck next time !" << endl;
		}

Except is always ignores the cin >> input it always goes stright to better lucknext tiem, then displays the area again, so it ignores the NewUser.location-> Whichever.

I think ive sorted it,

I have to take the input as a string parse it to 1 character and then put that input back into the char value,

that way the buffer will be cleared and it wont go to false

hah

This question has already been answered. Start a new discussion instead.