developer10
developer10

Reputation: 1500

Create array of struct instances, then call function inside other struct passing array of objects (pointers)

I've already posted a question in relation to a problem similar to what I can expect on my college exam and now this is another concrete issue I'm facing, probably due to lacking crucial understanding of pointers.

The problem has several structs defined.

One of them is struct Question{}, which has pointer attributes, and also an array which is to hold all the answers given for that particular question. At the destination, I should be able to iterate over all the questions in order to display them one by one to the user.

When I'm instantiating the exam (this is a simulation of an admission exam), I need to pass the student's citizen ID number, and the exam Questions.

// pi._prijavljeniKandidati[1]->_JMBG is the ID number in question
// 'questions' is supposed to carry all the questions I've hard-coded
// to save myself from entering manually

pi.StartExam(pi._prijavljeniKandidati[1]->_JMBG, questions);

This is how I tried it:

Question* questions = new Question;

// this initializes a single question
// 'answers' is the attribute that is holding all the answers
// the correct answer is BTW determined by an integer that is also
// sent in the below function

char* answers1[4];
answers1[0] = "London";
answers1[1] = "Berlin";
answers1[2] = "Helsinki";
answers1[3] = "Rome";

questions[0].Create("What is the capital of Finland?", answers1, 2);

// another question
char* answers2[3];
answers2[0] = "Ljubljana";
answers2[1] = "Paris";
answers2[2] = "Prague";

questions[0].Create("What is the capital of France?", answers2, 1);

And this is how the StartExam function actually looks like, nothing special here though, except that it shows how I tried getting some values of certain questions (based on the index thereof):

// I also tried void PokreniIspit(char* ID, Question* questions[])
void StartExam(char* ID, Question* questions)
{
    // this is just some dummy code line, to make sure it works
    cout << questions[1]._txtOfQuestion << endl;
}

When I run the app, the console crashes. Is there anything obvious that would make it crash?

For the sake of completeness, here's the whole Question structure:

// THIS IS HOW I IMAGING THIS STRUCT 'VISUALLY'
//= _txtOfQuestion ["Koji je glavni grad Njemacke?"]
//= _answers[10] //max 10 answers
//==== [0] Peking
//==== [1] London
//==== [2] Berlin
//==== [3] Seattle
//==== [4] Ljubljana
//= _posOfCorrect [2]
//= _points [4]

struct Question{

    char* _txtOfQuestion;
    char* _answers[4];
    int _posOfCorrect;
    int _points;

    void Unos(char* txt, char* answers[], int posCorrect, int points)
    {
        _txtOfQuestion= new char[strlen(txt) + 1];
        strcpy_s(_txtOfQuestion, strlen(txt) + 1, txt);

        for(int i = 0; i < 4; i++){
            _answers[i] = new char;
            strcpy_s(_answers[i], strlen(_answers[i]) + 1, _answers[i]);
        }

        _posOfCorrect = posCorrect;
        _points = points;

}

Upvotes: 1

Views: 378

Answers (2)

developer10
developer10

Reputation: 1500

I did initially try initializing Question like this:

Question* questions = new Question[2];

I'm back to using that because I do need array of questions.

But the real culprit (and why the console broke) was that I had this for loop with the 4 hardcoded in it.

As soon as I made my second question contain 4 options/answers, just like the first one - it worked.

for (int i = 0; i < 4; i++){
    _odgovori[i] = new char;
    strcpy_s(_odgovori[i], strlen(odgovori[i]) + 1, odgovori[i]);
}

Upvotes: 0

mch
mch

Reputation: 9804

void Unos(const char* txt, const char* answers[], int posCorrect, int points)
{
    if (points < 4)
        points = 4; //max 4 answer possibilities

    _txtOfQuestion= new char[strlen(txt) + 1];
    strcpy_s(_txtOfQuestion, strlen(txt) + 1, txt);

    for(int i = 0; i < points; i++){
        _answers[i] = new char[strlen(answers[i]) + 1];
        strcpy_s(_answers[i], strlen(answers[i]) + 1, answers[i]);
    }
    _posOfCorrect = posCorrect;
    _points = points;
}

I changed a little bit:

Since you call the function with string literals, the type of this parameters should be const char*.

You should limit the points parameter (What happens if someone call it with 5 answers?).

The for loop should run from 0 to points. It would be undefined behaviour if it runs till 4 and there are only 3 answers.

You need an array of chars, not only 1 char to save the answer.

There were some typos in the for loop (answers is not the same as _answers).

I suggest to use std::string instead of char* and a std::vector or similar instead of the array.

You should call the function like:

questions[0].Create("What is the capital of Finland?", answers1, 2, 3);

there are only 3 answers, so you pass a 3 as last parameter.

Upvotes: 0

Related Questions