0

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!

2
  • 3
    Do not employ using directives in headers, that forces all your users to share your using directive, whether they want or not, and may cause name collisions in the future. Commented Nov 30, 2011 at 12:22
  • @DavidRodríguez-dribeas oh Thanks for letting me know Commented Nov 30, 2011 at 12:40

2 Answers 2

4

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] << " " ;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much for the detailed response, a lot of new syntax/concepts to learn (haven't encountered constructor creation like that before) but it works :) I'll do some research but maybe you could explain to me what happens when you use size() on studentAnswers if its not to much trouble because i don't really understand how that's working.
@Holy: .size() returns the number of character in the string. And you use index i to access each character, one by one. Try printing std::cout << sheet.studentAnswers.size(), it will print 5, because there are 5 characters in "abadc" which was used to initialize studentAnswers.
@Holly: To be precise, there are 6 characters in "abadc", the 6th one is '\0', but that is not considered in .size() member function of std::string.
0

I don't think you need to switch, using array of char is okay(or you can use std::vector). When you use

char* something[5];

you just initialize array of pointers. And when you use

char something[5]; 

you get pointer to array - "something"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.