How can i define an array of char* in a struct in a separate header file?

i'm just beignning to learn about structs and seperating things into different files.

At the moment i have a Main.cpp file like so:

#include <iostream>
    #include "StudentAnswerSheet.hpp"
using std::cout;
using std::endl;

int main(){

    StudentAnswerSheet sheet = {
        "Sally",
        {'a', 'b', 'a', 'd', 'c'}
    };

    cout << sheet.studentName << ":" <<endl;
    for(int i = 0; i <5; i++){
    cout << sheet.studentAnswers[i] << " " ;
    }
    return 0;
}

and a separate header file that contains my struct StudentAnswerSheet data type:

#include <string>
using std::string;

struct StudentAnswerSheet{
    string studentName;
    char studentAnswers[5];
};

Ideally i'd like to be able to have an array of UP TO 5 chars to hold student answers. I think i might need to change from char to char* to get a degree of flexibility but when i tried implementing it i got an error message "too many intialisers for char [0]" and wasn't sure how to change the sheet intialization.

I'm also not sure what the best way to go about keeping track of how many elements my array contains if i switch to an array of char*.. if i take in the student answers with cin then i can keep track of the number of answers up to 5 but if i just wanted to initialize the answers myself like i am at the moment for testing im not sure what the most efficent way to calculate the size of studentAnswers would be, so any advice on that would be much appreciate too.

Thanks for any help!

Answers


Since it seems you're allowed to use std::string, then why dont you use std::vector<char> instead of using char[5] or thinking of using char* for flexibility? In your case, you can simply use std::string and then interpret the each character in it as student answer.

Also, since StudentAnswerSheet is not POD, which means the following would give compilation error, unless you use C++11:

//error in C++98, and C++03; ok in C++11
StudentAnswerSheet sheet = {
    "Sally",
    {'a', 'b', 'a', 'd', 'c'}
};

Here is what I would do:

struct StudentAnswerSheet
{
    std::string studentName;
    std::string studentAnswers;

    //constructor helps easy-initialization of object!
    StudentAnswerSheet(const std::string & name, const std::string & answers) 
                : studentName(name), studentAnswers(answers) {}
              //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
};            // it is called member-initialization list

then use it as:

StudentAnswerSheet sheet("Sally", "abadc");//easy: thanks to the ctor!

std::cout << sheet.studentName << std::endl;
for(size_t i = 0; i < sheet.studentAnswers.size(); ++i)
{
     std::cout << sheet.studentAnswers[i] << " " ;
}

Need Your Help

Possibility of Inserting iphone contact with Deleted ABRecordID?

iphone objective-c

I was wondering if it possible to add iPhone Contact with Deleted ABRecordID.

Groovy singleton thread safe

groovy thread-safety singleton

Does @Singleton annotation in Groovy make a singleton thread-safe?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.