Question: C++ I am stuck in making my program do the following: -Make Employee and Department editable. That means, the user should be able to edit
C++
I am stuck in making my program do the following:
-Make Employee and Department editable. That means, the user should be able to edit a given Employee and Department.
-Do not allow the user to edit the Employee ID and Department ID.
------------------------
#include
using namespace std;
//1st struct declaration struct Department { string departmentID; string departmentName; string departmentHeadName; //Does not allow to change employeeID and departmentID public: void changeValues(string name, string head) { departmentName=name; departmentHeadName=head; } }; //2nd struct declaration struct Employee { string employeeID; string employeeName; double employeeSalary; int employeeAge; string employeeDepartmentID; //Does not allow to change employeeID and departmentID public: void changeVals(string name, int age, double salary) { employeeName=name; employeeAge=age; employeeSalary=salary; } }; //Main function int main() { //Variables Employee e[7]; //Employee array with size 7 Department d[5]; //Department array with size 5 int choice = 0; int employeeAge = 0; int i = 0; int j = 0; string departmentID; string departmentName; string departmentHeadName; string employeeID; string employeeName; string employeeDepartmentID; double employeeSalary = 0.0; ifstream inputFile; ofstream outputFile; string dhn; string Id; string dn; string emPd; double es; int ea; string en; string ed;
do { cout << " 1. Create Department 2. Create Employee"; cout<< " 3. Retrieve DEPARTMENT data from file 4. Retrieve EMPLOYEE data from file"; cout << " 5. Display Report"; cout << " 6. Exit Enter your choice: "; cin >> choice;
switch (choice) { case 1: if (i < 3) {
cout << " Enter the department ID: "; cin.ignore(); //Skip the newline character getline(cin, departmentID); d[i].departmentID = departmentID;
cout << " Enter the department name: "; getline(cin, departmentName); d[i].departmentName = departmentName;
cout << " Enter the department head name: "; getline(cin, departmentHeadName); d[i].departmentHeadName = departmentHeadName;
/* Random Access Files, writes the data to the file right away*/ string line = departmentID + " " + departmentName + " " + departmentHeadName + " "; ofstream outputFile("output.txt", ios::app); outputFile << line; outputFile.close(); i++; } else cout<< " The array is full, you can not add any more departments "; break;
case 2: if (j < 7) { cout << " Enter the employee ID: "; cin.ignore(); getline(cin, employeeID); e[j].employeeID = employeeID; cout << " Enter the employee name: "; getline(cin, employeeName); e[j].employeeName = employeeName;
cout << " Enter department ID: "; getline(cin, departmentID); e[j].employeeDepartmentID = departmentID;
cout << " Enter the employee age: "; cin >> employeeAge; e[j].employeeAge = employeeAge;
cout << " Enter the employee salary: $"; cin >> employeeSalary; e[j].employeeSalary = employeeSalary;
/* Random Access Files, writes the data to the file right away*/ string line1 = employeeID + " " + employeeName + " " + departmentName + " "; ofstream outputFile("output1.txt", ios::app); outputFile << line1; outputFile << employeeAge << " " << employeeSalary << " "; outputFile.close(); j++; } else cout<< " The array is full, you can not add any more Employees "; break;
case 3: cout << " Reading DEPARTMENT data from file "; { ifstream inputFile; inputFile.open("output.txt"); for (int k = 0; k < 3; k++) { cout << ' '; inputFile >> dn; d[k].departmentHeadName = dn; cout << "Department Head Name: " << d[k].departmentHeadName << ' '; inputFile >> Id; d[k].departmentID = Id; cout << "Department ID: " << d[k].departmentID << ' '; inputFile >> dhn; d[k].departmentName = dhn; cout << "Department Name: " << d[k].departmentName << ' '; } inputFile.close(); break; }
case 4: cout << " Reading EMPLOYEE data from file "; { ifstream inputFile; inputFile.open("output.txt"); for (int l = 0; l < 7; l++) { cout << ' ';
inputFile >> emPd; e[l].employeeDepartmentID = emPd; cout << "Employee Department ID: " << e[l].employeeDepartmentID << ' ';
inputFile >> es; e[l].employeeSalary = es; cout << "Employee Salary: $" << e[l].employeeSalary << ' ';
inputFile >> ea; e[l].employeeAge = ea; cout << "Employee Age: " << e[l].employeeAge << ' ';
inputFile >> en; e[l].employeeName = en; cout << "Employee Name: " << e[l].employeeName << ' ';
inputFile >> ed; e[l].employeeID = ed; cout << "Employee ID: " << e[l].employeeID << ' '; } inputFile.close(); break; }
case 5: cout << " Report: "; { double totSalary = 0; for (j = 0; j < 7; j++) { totSalary = totSalary + e[j].employeeSalary; } cout<< " The total salary of all the departments employees is: $" << totSalary << ' '; break; } case 6: cout << " Exiting: "; { char exitChoice;
cout << "ALERT. Data has not been saved and will be lost."; cout << ' '; cout << " Do you wish to continue (Y/N)? "; cin >> exitChoice;
if (exitChoice == 'Y' || exitChoice == 'y') { cout << "Thank You. Good Bye"; cout << ' ' << ' '; exit(0); break; } else if (exitChoice == 'N' || exitChoice == 'n') { } } break; default: cout << " Select Option: " << ' '; break; } } while (choice != 7); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
