asked 75.2k views
4 votes
I have this code so far

#include
#include
#include
#include
using namespace std;
const int MAX_RECORD = 100;
struct instructers{
int instructorID;
int salary;
char middleInital;
string payrollCode;
string firstName;
string lastName;
string departmentCode;
};
int main() {
ifstream file;
file.open("pa2data");
if(!file.is_open()){
cout << "Error opening the file";
return 1;
}else{
}
instructers* instructors[MAX_RECORD];
char record[MAX_RECORD];
int numInstructors = 0;
for (int i = 0; i < MAX_RECORD; i++) {
instructors[i] = nullptr;
}
if(file.is_open()){
file.read(record, MAX_RECORD);
while (!file.eof()) {
char CfirstName[11], CmiddleName[1], ClastName[15], CdeptCode[5], Csalary[6];
for (int i = 0; i < 10; i++) {
CfirstName[i] = record[i];
}
CfirstName[10] = '\0';
for (int i = 10, j = 0; i <= 11; i++, j++) {
CmiddleName[j] = record[i];
}
CmiddleName[11] = '\0';
for (int i = 20, j = 0; i <= 39; i++, j++) {
ClastName[j] = record[i];
}
ClastName[20] = '\0';
for (int i = 40, j = 0; i <= 48; i++, j++) {
CdeptCode[j] = record[i];
}
CdeptCode[9] = '\0';
for (int i = 49, j = 0; i <= 51; i++, j++) {
Csalary[j] = record[i];
}
Csalary[3] = '\0';
string fNameS, lNameS, studIDS;
int salaryS;
fNameS = CfirstName, lNameS = ClastName, studIDS = CdeptCode;
salaryS = atoi(Csalary);
instructors[numInstructors] = new instructers;
instructors[numInstructors]->firstName = fNameS;
instructors[numInstructors]->middleInital = CmiddleName[0];
instructors[numInstructors]->lastName = lNameS;
instructors[numInstructors]->departmentCode = CdeptCode[0];
//instructors[numInstructors]->instructorID = studIDS.substr(1, 8);
instructors[numInstructors]->salary = salaryS;
// cout << ClastName << endl;
numInstructors++;
file.read(record, MAX_RECORD);
}
file.close();
}
else {
cout << "Error. File failed to open.";
return 1;
}
numInstructors = 0;
while (instructors[numInstructors] != nullptr) {
cout << "E [" << numInstructors << "] Name: " << instructors[numInstructors]->firstName << " " << instructors[numInstructors]->middleInital << " " << instructors[numInstructors]->lastName << endl;
numInstructors++;
}
for (int i = 0; instructors[i] != nullptr; i++) {
delete instructors[i];
}
return 0;
}
need to fix characters since
Instructor ID
payroll code 3 characters
number 6 digits
First name 10 characters
Middle initial 1 character
Last Name 15 characters
Department code 5 characters
Salary 6 digits
and then
salary function
Code a function named salary, which has two parameters and a return value.
The parameters are:
1) the Instructor pointer array
2) a pointer variable of type Instructor
The return value is an int.
salary performs two bits of analysis. It loops through the Instructor objects, calculating the average salary. It
also determines which Instructor has the highest salary. You can perform both analyses in the same loop. The
average salary is passed back to the caller as the return value. The address of the Instructor object with the
highest salary is passed back to the caller in the second parameter.
Main’s structure:
open the input file – if it fails to open, display an error message and terminate
read the file and populate the Instructor objects
display the Instructor data with appropriate column headers and formatting to make it readable
invoke salary
display the average salary and the name of the instructor with the highest salary
clean up (deallocate memory, close files)
return
Source code file structure
Use the standard approach to source files in this assignment.
• a .cpp for each function. You’ll have at least two – main and salary. You could have more if you design
your project with more functions.
• a header file for the Instructor class / struct, properly guarded.
• a header file for the function prototype for salary, properly guarded.
Debugging
For debugging purposes, it would be a good idea to code your program in steps. First, write code to read the
input and create the Instructor objects. Determine if your parsing works by displaying each Instructor as it’s
created (but remove this display before you submit for a grade). Make sure that if you read n input records,
you actually create n objects. Verify that the n+1th pointer in the Instructor pointer array contains nulls.
Then, add the code to produce the report from the Instructor objects. Finally, code the salary function, call it,
and display its results.

asked
User Erdeszt
by
8.3k points

1 Answer

1 vote

Answer:

```cpp

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

const int MAX_RECORD = 100;

struct Instructor {

int instructorID;

int salary;

char middleInitial;

string payrollCode;

string firstName;

string lastName;

string departmentCode;

};

int salary(Instructor* instructors[], Instructor** highestSalaryInstructor) {

// Calculate average salary and find the highest salary instructor

// (code for loop and calculations here)

}

int main() {

ifstream file("pa2data");

if (!file.is_open()) {

cout << "Error opening the file";

return 1;

}

Instructor* instructors[MAX_RECORD];

// ... Rest of your code for parsing file and creating Instructor objects ...

Instructor* highestSalaryInstructor = nullptr;

int avgSalary = salary(instructors, &highestSalaryInstructor);

cout << "Average Salary: " << avgSalary << endl;

if (highestSalaryInstructor != nullptr) {

cout << "Instructor with the highest salary: " << highestSalaryInstructor->firstName << " " << highestSalaryInstructor->middleInitial << " " << highestSalaryInstructor->lastName << endl;

}

// Clean up and close the file

// ...

return 0;

}

```

answered
User Nicole Finnie
by
8.8k points