Classes Lab 3: Arrays of Objects A small university has no more than 250 faculty
ID: 3607333 • Letter: C
Question
Classes Lab 3: Arrays of Objects A small university has no more than 250 faculty members. An input file has data for faculty, using one line per person. Each line has first name, last name, department, salary, and number of years of service, separated by spaces. The main program will process an array of objects of type facultyType. Use the header file "facultyType.h" written for the facultyType lab of October 30. The main program will use functions to do each of the following: 1. Get input from the data file for an array of facultyType objects; the input function will determine how many objects are in the array. 2. Output the faculty information, in a loop using the class member function print0 ch )3. Add a cost of living increase of 10% to the salary of every faculty member 4. Output the faculty information, in a loop using the class member function print Output the faculty information of those who have more than 15 years of service 5. S uctExplanation / Answer
char *department;
long salary;
int numOfYears;
};
int main()
{
int n;
char line[100];
fr = fopen ("test.txt", "r"); /* open the file for reading */
// as we already know that there are less than 250 faculties, we pre declare 250 size array
FacultyDetails collegeFaculties[250];
int curr_size=0;
while(fgets(line, 100, fr) != NULL)
{
// strtok is used to tokenize the string using delimeter which here is ,
char *p = strtok(line, ",");
// since we already know that there are 5 fields in each line, we tokenise 5 times
collegeFaculties[curr_size].firstName=p;
p = strtok(NULL, ",");
collegeFaculties[curr_size].lastName=p;
p = strtok(NULL, ",");
collegeFaculties[curr_size].department=p;
p = strtok(NULL, ",");
collegeFaculties[curr_size].salary=stringToInt(p);
p = strtok(NULL, ",");
collegeFaculties[curr_size].numOfYears=stringToInt(p);
p = strtok(NULL, ",");
curr_size++;
}
// print the number of faculties
cout<<"The number of objects are "<<curr_size<<endl;
// directly print the array
for(int i=0;i<curr_size;i++) {
cout<<collegeFaculties[i].firstName<<" "<<collegeFaculties[i].lastName<<" "<<collegeFaculties[i].department<<" "<<collegeFaculties[i].salary<<" "
<<collegeFaculties[i].numOfYears<<endl;
}
// increment the salary by 10%
for(int i=0;i<curr_size;i++) {
collegeFaculties[i].salary = collegeFaculties[i].salary*1.10;
}
// print all the college faculties with increased salary
for(int i=0;i<curr_size;i++) {
cout<<collegeFaculties[i].firstName<<" "<<collegeFaculties[i].lastName<<" "<<collegeFaculties[i].department<<" "<<collegeFaculties[i].salary<<" "
<<collegeFaculties[i].numOfYears<<endl;
}
// check if their number of years is > 15, if so print else continue on
for(int i=0;i<curr_size;i++) {
if(collegeFaculties[i].numOfYears > 15) {
cout<<collegeFaculties[i].firstName<<" "<<collegeFaculties[i].lastName<<" "<<collegeFaculties[i].department<<" "<<collegeFaculties[i].salary<<" "
<<collegeFaculties[i].numOfYears<<endl;
}
}
fclose(fr); /* close the file prior to exiting the routine */
return 0;
}