Alex Shaw
Alex Shaw

Reputation: 11

Defining a character array in header file in C++

I am working on a project where a question is displayed in a game window.

Since this question will need to change a lot, I figure it would be easier to have 5 defined lines of text (1 for question, 4 for MC answers) that are simply edited every time the question changes.

I have tried this in the header file:

struct tagQuestion{
    int x, y;
    const char* qLine[150];
    char ansA[150];
    char ansB[150];
    char ansC[150];
    char ansD[150];

}question[1];

then in my main.cpp

question.qLine[150] = "TEST PHRASE";

but it is returning the error "qLine" in "question", which is of non-class type "tagQuestion[1]"

I have tried both char and const char* to no success.

I am trying to follow an example in my textbook and I think I'm misunderstanding it.

Once I declare the character array in the header file, can't I edit its contents in the main file?

Upvotes: 0

Views: 1456

Answers (1)

Max Vollmer
Max Vollmer

Reputation: 8598

  1. That [1] after question doesn't seem to be making any sense. Remove it.

  2. Your answers are arrays of char, but your question is an array of char pointers. I am pretty sure you don't want your question to consist of 150 char pointers.

  3. If you want to change qLine, don't make it const.

  4. qLine[150] = ... This assigns a value to the 151st element of an array with 150 elements. Neither what you want, nor legal code. You probably want qLine =, but that doesn't work with an array, instead you need to use a function like strcpy that writes data into the array, but I highly advise against that. (see next point)

  5. Don't do C in C++. Use std::string, not arrays of char.

  6. If you have strings that regularly change, create a file where your strings are stored, load them into a map, and pull them from the map wherever and whenever you need them, instead of putting hardcoded strings in your code. That way you don't need to change any code, when you change text.

I suggest something like this:

struct Question {
    std::string text;
    std::vector<std::string> answers;
};

std::map<std::string, Question> questions;

And then something like this:

ifstream questionsFile("questions.txt");
if (questionsFile.is_open())
{
    std::string line;
    while (std::getline(questionsFile, line))
    {
        // Split line into some id, text and answers, how exactly depends on the format you chose
        std::string id = ...;
        Question question;
        question.text = ...;
        question.answers.push_back(...);
        questions[id] = question;
    }
    questionsFile.close();
}
else
{
    // Handle error
}

And wherever in your code:

// Print question with id "q1":
std::cout << questions["q1"].text << std::endl;

// Print answer #2 for question with id "q1":
std::cout << questions["q1"].answers[1] << std::endl;

Even better would be to make Question a class that hides its internals and has accessor methods and a constructor or static method to create new instances from valid lines with proper error handling.

Upvotes: 1

Related Questions