/* This program demonstrates in-stack 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); // function to calculate the class average double classAverage(Student arr[], int size); int main() { /* We can create an array of structs. */ Student arr[10]; int num; cout<<"Enter the number of students: "; cin>>num; cout<<"enter student data: "; for(int i=0; i< num; i++) { cin.ignore(); // Remove the preceding newline readStudent(arr[i]); // use same as previous array } cout<<"Printing student details: "<, not the dot operator */ Student *s = new Student; // We need to dereference to pass by reference cout<<"enter the student data: "; readStudent(*s); printStudent(*s); cout<name <<" is "<< 2022 - s->dateOfBirth.year<<" years old"<>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; }