Exercise 8 has us re-writing a skeleton program to fill in the missing functions. A combination of structs, pointers, and functions will complete this exercise. Comments describing where and what additions have been made are embedded in the code. See my solution below:
8. This exercise provides practice in writing functions dealing with arrays and structures. The following is a program skeleton. Complete it by providing the described functions:
#include <iostream> using namespace std; const int SLEN = 30; struct student { char fullname[SLEN]; char hobby[SLEN]; int ooplevel; }; int getinfo(student pa[], int n); void display1(student st); void display2(const student * ps); void display3(const student pa[], int n); int main(){ cout << "Enter class size: "; int class_size; cin >> class_size; while (cin.get() != '\n') continue; student * ptr_stu = new student[class_size]; int entered = getinfo(ptr_stu, class_size); for (int i = 0; i < entered; i++) { display1(ptr_stu[i]); display2(&ptr_stu[i]); } display3(ptr_stu, entered); delete [] ptr_stu; cout << "Done\n"; return 0; } // getinfo() has two arguments: a pointer to the first element of // an array of student structures and an int representing the // number of elements of the array. The function solicits and // stores data about students. It terminates input upon filling // the array or upon encountering a blank line for the student // name. The function returns the actual number of array elements // filled. int getinfo(student pa[], int n) { int i; for(i = 0; i < n; i++) { cout << "Student Number: " << (i+1) << "\n"; cout << "Student's Name: "; cin.getline(pa[i].fullname, SLEN); cout << "Enter students hobby: "; cin.getline(pa[i].hobby, SLEN); cout << "Enter students OOP level: "; cin >> pa[i].ooplevel; cin.get(); } return i; } // display1() takes a student structure as an argument // and displays its contents void display1(student st) { cout << "\n=============Display 1================\n"; cout << "Student name: " << st.fullname << "\n"; cout << "Hobby: " << st.hobby << "\n"; cout << "OOP Level: " << st.ooplevel << "\n"; cout << "=============End Display 1==============\n"; } // display2() takes the address of student structure as an // argument and displays the structure's contents void display2(const student * ps) { cout << "\n============Display 2================\n"; cout << "Students Name: " << ps->fullname << "\n"; cout << "Hobby: " << ps->hobby << "\n"; cout << "OOP Level" << ps->ooplevel << "\n"; cout << "==========End Display 2================\n"; } // display3() takes the address of the first element of an array // of student structures and the number of array elements as // arguments and displays the contents of the structures void display3(const student pa[], int n) { for(int i = 0; i < n; i++) { cout << "\n============Display 3================\n"; cout << "Student: " << (i+1) << "\n"; cout << "Fullname: " << pa[i].fullname << "\n"; cout << "OOP Level: " << pa[i].ooplevel << "\n"; cout << "=============End Display 3============\n"; } }