/* This program demonstrates dynamic arrays of structs */ #include #include #include using namespace std; // We use the same structs from last class struct Date { int month, day, year; char dayOfWeek[15]; }; /* Student structure * name - string * major - string * GPA - float * EMPL - long * grades - array of 3 floats * datOfBirth - Date struct variable */ struct Student { string name, major; float gpa; long empl; float grades[3]; Date dateOfBirth; }; // If a function uses a structure, it has to be declared after the struct declaration // We use the same functions from last class void printDate(Date d); Date readDate(); void printStudent(Student s); void readStudent( Student &s); int main() { /* We can create a dynamic array of structs. Once the array is created, we can * use it just like an in-stack array */ int num; cout<<"Enter the number of students for the dynamic array: "; cin>>num; Student *sPtr = new Student [num]; for(int i=0; i< num; i++) { cin.ignore(); // Remove the preceding newline readStudent(sPtr[i]); // use same as previous array } double sum =0; /* Calculating class average for this array - same logic as before */ for(int i=0; i>var.month>> junk>> var.day>>junk>>var.year; return var; } /* This function accepts a structure variable and prints out the details in the structure * We use the dot (.) operator to access individual elements. */ void printStudent( Student s) { cout<>s.gpa; cout<<"EMPLID: "; cin>>s.empl; cout<<"3 grades: "; for(int i=0; i<3;i++) cin>>s.grades[i]; cin.ignore(); // remove newline after entering grades cout<<"Date of birth (Day of Week, m/d/y): "; cin.getline(s.dateOfBirth.dayOfWeek, 15, ','); cin>>s.dateOfBirth.month; cin.ignore(); cin>>s.dateOfBirth.day; cin.ignore(); cin>>s.dateOfBirth.year; }