I've googled relates to struct and I was able to see how they are used; however, I couldn't clearly figure out some of them.
Let's say I have 2 structs
struct Student {
int age;
int height;
};
struct School {
Student information;
};
and let's say I want to handle information School[i].Student[j].age or height based on input file.
int main() {
int school_number = 20;
int student_number = 50;
School school[school_number-1]; // is this the correct way to create it? since [0] is the first school
for (int i=0; i < school_number; i++) {
for (int j=0; j < student_number; j++) {
getline(file, line); // let's say the line has student's age and height.
istringstream c(line);
c >> school[i].information[j].age >> school[i].information[j].height;
}
}
}
I thought this would do the job, but I'm getting no match for 'operator[]' (operand types are 'Student' and 'int') compile error.
What am I missing?
when it's just student,
Student info[student_number-1];
for (int i=0; i < student_number; i++) {
getline(file, line);
istringstream c(line);
c >> information[i].age >> information[i].height;
}
this one works without problem, but I am still not sure what I need to do for 2 structs, where one is calling other one.
One more question,
while I was searching, I see lots of
School *id = new School[school_number-1];
something like this. How does this different from
School school[school_number-1];
this one?
I see a pointer, so I'm pretty sure it does something, but based on how they are used, they look pretty much same.
edit : I've tried little bit, but still not sure how to use vector in this case.
for the above case,
int main() {
vector[Student] student;
int school_number = 20;
int student_number = 50;
School school[school_number-1]; // is this the correct way to create it? since [0] is the first school
for (int i=0; i < school_number; i++) {
for (int j=0; j < student_number; j++) {
getline(file, line); // let's say the line has student's age and height.
istringstream c(line);
c >> school[i].information[j].age >> school[i].information[j].height;
}
}
}
if I call
vector[Student] student;
how can I modify the line
c >> school[i].information[j].age >> school[i].information[j].height;
with the variable student I just created?