Reputation: 635
I'm completely stumped. How do I fill a struct
array that is contained inside another struct
?
I have the two struct
s:
struct employee_stats{
char emp_name[MAX_NAME_LENGTH];
double salary;
int years_employed;
}
struct annual_reviews{
int year;
struct employee_stats employee[NO_EMPLOYEES];
}
I declare a pointer to an array of annual_reviews
structs
annual_reviews *sort_records = new annual_reviews[num_years];
I have a vector
of strings that read from a text file earlier in the program. I send the employee_record
and year
vectors to a method to fill the two struct
s
void DataSort::load_records(vector<string> employee_record, vector<string> year){
vector<string> line_data;
string word;
//split employee_record by \t and into single line string vector
for(int i = 0; i < employee_record.size(); i++){
stringstream wordstream(employee_records[i]);
while(getline(wordstream, word, '\t')){
if(!word.empty())
line.push_back(word);
}
}
Now , within the same method, I want to add this data to my struct
s:
for(int j = 0; j < num_years; j++){
sort_records[i].year = atoi(year[i].c_str); //year of record
for(int k = 0; k < NO_EMPLOYEES; k++){
//here is where it all falls apart
sort_records[j].employee_stats[j].emp_name = line[j]; //fill the records
}
}
}
I realize I'm not accessing the inside struct
correctly but I'm stuck, I've hit a wall.
I get two compilation errors using VS2015:
std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str':non-standard syntax;use'&'to create a pointer to member
array type 'char[25]' is not assignable
Can someone point me in the right direction? Do I need to create a pointer to the member struct
? I thought you could index the members directly.
Thanks.
Upvotes: 1
Views: 7772
Reputation: 903
struct used for making datatype.
struct employee_stats{
char emp_name[MAX_NAME_LENGTH];
double salary;
int years_employed;
}
struct annual_reviews{
int year;
struct employee_stats[NO_EMPLOYEES];
}
on the annual_reviews datatype, i think you declare another datatype by using
struct employee_stats
so i think you should declare it as a data array as you want it on the second one
struct employee_stats{
char emp_name[MAX_NAME_LENGTH];
double salary;
int years_employed;
}
struct annual_reviews{
int year;
employee_stats employeeData[NO_EMPLOYEEs];
}
Here after making variable type annual_reviews, i think you can access the employeeData array. And then for the char error, i think you should use string as dataType on emp_name.
[EDIT] copy string to char
strcpy(sort_records[j].employee_stats[j].emp_name, line[j].c_str());
Reading source : http://www.cplusplus.com/reference/string/string/c_str/
Upvotes: 1
Reputation: 6255
You problem is that you are not declaring the inner structure correctly.
You code should look like some thing like this (pseudo-code):
typedef employee_stats some_type;
Use some_type inside annual_reviews.
The rest is trivial.
Upvotes: 0