// Illustrates pass by address with structures #include #include using namespace std; const int SIZE = 5; const int MAXNAME = 20; struct Student { char fName[MAXNAME]; // first name char lName[MAXNAME]; // last name int socSecNumber; // social security number double gpa; // grade point average }; // some functions void LoadStudentData(Student* sp); void PrintStudentArray(const Student* sArray); void PrintOneStudent(const Student& sp); int main() { Student sList[SIZE]; // array of 5 students int i; for(i = 0; i < 5; i++) { cout << "Entering data for student " << i+1 << "... \n"; LoadStudentData(&sList[i]); } PrintStudentArray(sList); return 0; } void LoadStudentData(Student* sp) { cout << "Enter first name: "; cin.getline(sp->fName, MAXNAME); cout << "Enter last name: "; cin.getline(sp->lName, MAXNAME); cout << "Enter SSN: "; cin >> sp->socSecNumber; cout << "Enter GPA: "; cin >> sp->gpa; // consume remaining characters up through newline. while (cin.get() != '\n') ; } void PrintOneStudent(const Student& s) // prints the internal data of any student structure passed in // structure is passed in by const reference (avoids making copy) { cout << left; cout << setw(20) << s.fName << ' ' << setw(20) << s.lName << " " << setw(10) << s.socSecNumber; cout << " GPA: " << right << fixed << setprecision(2) << setw(4) << s.gpa << '\n'; } void PrintStudentArray(const Student* sArray) { cout << "\n\nPrinting student records\n\n"; for (int i = 0; i < SIZE; i++) PrintOneStudent(sArray[i]); }