Home > Software design >  C Exception Thrown while trying to debug
C Exception Thrown while trying to debug

Time:10-25

I am trying to learn C with a given tutorial.

I've tried to write some code. Visual studio says, there's no error, but when I'm trying to start debugging, it does not work. Can someone help me.

I am getting the following Exception thrown.

Here's the error message:

https://prnt.sc/1x6qqgc

#include <iostream>
#include <string>
using namespace std;

class Question
{
private:
    string text;
    string choices[3];
    string answer;
public:
    Question(string text, const string choices[3], string answer)
    {
        this->text = text;
        this->choices[3] = choices[3];
        this->answer = answer;
    }
    bool checkAnswer(string answer)
    {
        return this->answer == answer;
    }
    string getText()
    {
        return this->text;
    };

};

class Quizz
{
private:
    Question* questions[5];
    int score = 0;
    int questionIdx = 0;
public:
    Quizz(Question questions)
    {
        this->questions[5] = &questions;
    };

    Question* getQuestions()
    {
        return questions[questionIdx];
    };

    void displayQuestion()
    {
        Question* questions = getQuestions();
        cout << questionIdx   1 << " / " << 5 << "Question : " << questions->getText();
    };

};

int main()
{
    const string cho[3] = { "Carl","Mike","Jason" };

    Question q1 = {"Who has a dog?", cho, "Carl"};
    Question q2 = { "Who has a cat", cho , "Mike" };
    Question q3 = { "Who knows i have a cat ", cho , "Mike" };
    Question q4 = { "Who knows i haven't got a dog", cho , "Carl" };
    Question q5 = { "Who knows i live in LA", cho , "Jason" };

    Question questions[5] = { q1,q2,q3,q4,q5 };

    Quizz quiz(questions[5]);

    quiz.displayQuestion();

    return 0;
}

CodePudding user response:

You have out-of-array-boundries access everywhere:

this->choices[3] = choices[3]; --> You can only access up to choices[2]
this->questions[5] = &questions; --> You can only access up to questions[4]
Quizz quiz(questions[5]);

Note: Counting in arrays starting at index 0 means if you define string choices[3]; that means you have 3 strings at choices[0], choices[1] & choices[2]

CodePudding user response:

It's solved, thank you for your helps.

#include <iostream>
#include <string>
using namespace std;

class Question
{
private:
    string text;
    string choices[3];
    string answer;
public:
    Question(string textt, string choices[] , string answerr)
    {
        text = textt;
        for (int i = 0; i < 3 ; i  ) {
            this->choices[i] = choices[i];
        }

        answer = answerr;

    }
    bool checkAnswer(string answer)
    {
        return this->answer == answer;
    }
    string getText()
    {
        return this->text;
    };

};

class Quizz
{
private:
    Question* questions[5];
    int score = 0;
    int questionIdx = 0;
public:
    Quizz(Question questions[])
    {
        for (int i = 0; i < 5; i  )
        {
            this->questions[i] = &questions[i];
        }
    };

    Question* getQuestions()
    {
        return questions[questionIdx];
    };

    void displayQuestion()
    {
        Question* questions = getQuestions();
        cout << questionIdx 1 << " / " << 5 << "Soru : " << questions->getText();
    };

};

int main()
{
    const string cho[3] = { "Carl","Mike","Jason" };

    Question q1 = {"Who has a dog?", cho, "Carl"};
    Question q2 = { "Who has a cat", cho , "Mike" };
    Question q3 = { "Who knows i have a cat ", cho , "Mike" };
    Question q4 = { "Who knows i haven't got a dog", cho , "Carl" };
    Question q5 = { "Who knows i live in LA", cho , "Jason" };

    Question questions[5] = {q1,q2,q3,q4,q5};

    Quizz quiz(questions);

    quiz.displayQuestion();

    return 0;
}

  • Related