Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Having trouble getting my code to pass requirements. Below is my code: /* Write a program to allow the user to: 1. Create two classes.

Having trouble getting my code to pass requirements.

Below is my code:

/* Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods.

2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to do the following: 1. Create Department. Collect all information about a department. Make sure the department ID does not already exist in the array containing Department objects. If it does not, then insert the Department object into the array. When the array is full, display the error message to the user "The array is full, you can not add any more departments" 2. Create Employee. Collect all information about an Employee. Make sure the Employee ID does not already exist in the array containing Employee objects. If it does not, then insert the Employee object into the array. Also make sure that the DepartmentID that the employee belongs also exists. If it does not, then display error message. When the array is full, display the error message to the user "The array is full, you can not add any more Employees" 3. Write the data to the file. When the user selects this option, dump the information in each array into a separate file. 4. Retrieve data from file. When user selects this option, open each file, load the data from the file into the appropriate array. 5. Display Report: When user selects this option, go through arrays and display the total salary paid for each department. The report should display the department name and the total salary of all employees in that department. */

// header files #include #include #include #include using namespace std;

// define class Deparments class Departments { // access specifier private: // data members int DepartmentID; string Departmentname; string DepartmentHeadName; // access specifier public: Departments(){} Departments(int inId,string name,string headName) {DepartmentID = inId; Departmentname = name; DepartmentHeadName = headName;} string getDepartmentHeadName() const {return DepartmentHeadName;} int getDepartmentID() const {return DepartmentID;} string getDepartmentname() const {return Departmentname;} // member functions void setDepartmentHeadName(string DepartmentHeadName) {this->DepartmentHeadName = DepartmentHeadName;} void setDepartmentID(int DepartmentID) {this->DepartmentID = DepartmentID;} void setDepartmentname(string Departmentname) {this->Departmentname = Departmentname;} };

// define class Employee class Employee { // access specifier private: // data members int employeeID; string emploeename; double employeesalary; double employeeage; int employeeDepartmentID; // access specifier public: Employee(){} Employee(int id,string name,double salary,double age,int dId) {employeeID = id; emploeename = name; employeesalary = salary; employeeage = age; employeeDepartmentID = dId;} string getEmploeename() const {return emploeename;} int getEmployeeDepartmentID() const {return employeeDepartmentID;} int getEmployeeID() const {return employeeID;} double getEmployeeage() const {return employeeage;} double getEmployeesalary() const {return employeesalary;} void setEmploeename(string emploeename) {this -> emploeename = emploeename;} void setEmployeeDepartmentID(int employeeDepartmentID) {this -> employeeDepartmentID = employeeDepartmentID;} void setEmployeeID(int employeeID) {this -> employeeID = employeeID;} void setEmployeeage(double employeeage) {this->employeeage = employeeage;} void setEmployeesalary(double employeesalary) {this->employeesalary = employeesalary;} }; int main() // array for Employee class size 5 {Employee employees[5]; // array for Department class size 3 Departments departments[3]; // set to variables to 0 int numDepartment = 0; int numEmployees = 0; int choice = 0; while(choice != 6)

// display menu to user {cout > choice; // condition for user input if (choice == 1) {if(numDepartment == 3) // display to user when array is full {cout > dId; bool validId = true;

for (int i = 0; i > dName; cout > dHeadName; Departments d(dId,dName,dHeadName); departments[numDepartment] = d; numDepartment ++;} else if(choice == 2) {if(numEmployees == 5) // display error message {cout

int eId; string eName; double eSalary; double eAge; int eDepartmentid; cout > eId; bool validId = true;

for(int i = 0; i

if(!validId) {continue;}

// prompt user for data cout > eName; cout > eSalary; cout > eAge; cout > eDepartmentid; bool foundId = false;

while(!foundId) {for(int i = 0; i

if(!foundId) {cout > eDepartmentid;}}

Employee e(eId,eName,eSalary,eAge,eDepartmentid); employees[numEmployees] = e; numEmployees++;} // option to write data to file1 and file2 else if(choice == 3) {ofstream file1 ("departments.txt"); ofstream file2 ("employees.txt"); for(int i=0; i

for(int i = 0; i

{while(getline(infile, line,' ') ) {stringstream ss(line); ss >> dId >> dName >> dHeadName; Departments d(dId,dName,dHeadName); departments[numDepartment] = d; numDepartment ++;} // close the file infile.close();}

//read employees ifstream infile2; int eId; string eName; double eSalary; double eAge; int eDepartmentid; numEmployees = 0; infile2.open ("employees.txt"); // open file if (infile2.is_open()) {while( getline(infile2, line,' ')) {stringstream ss(line); ss >> eId >> eName >> eSalary >> eAge >> eDepartmentid; Employee e(eId,eName,eSalary,eAge,eDepartmentid); employees[numEmployees] = e; numEmployees ++;} infile2.close();} } // display report else if(choice == 5) {string name = ""; double salary = 0; {cout > choice;}} // message to quit program cout

here is a screenshot of requirements my code is not passing

image text in transcribed

image text in transcribed

PRETTY DIFF This diff is colored to make it clear what parts of the output are wrong, Green indicates things in the correct output that you are missing, e indicates things in your output that shouldn't be there. Thecharacter re ers to new lines so he green character re ers a newline you are missing n your output and the red refers to a new ne you need to remove rom your output. Employee ID Employee Name: Salary: SAge Department ID Please enter a valid department ID: 1. Create Department 2. Create Employee 3. Write Out Data File 4. Read In Data File. 5. Display Salary Report 6.-Quit Please make a selection The array is full, you can not add any more Employees. 1. Create Department 2. Create Employee 3, Write Out Data File 4. Read In Data File 5. Display Salary Report+ 6. -_ Quit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions