简体   繁体   中英

How to store struct data in an array in C++?

I have this program for school it gets data about a student, does a few calculations and stores the data in a struct which is returned by the input function.

Right now I've only got it working for one student, but I need to be able to store and output data for more than one student.

"Right now I've only got it working for one student, but I need to be able to store and output data for multiple students."

Use std::vector

int n; //No. of student

std::vector<studentType> vec;
studentType s;

for(size_t i =0; i<n ;++i)
{
  s = input();
  vec.push_back(s);
}

And then you can access

vec[i].studentID ; // etc, for ith student

On another note, void main is not legal C++, use int main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM