#include #include #include #include /* "at least 65 employees" "almost double */ /* 65*2 = 130 */ /* a (dynamic) vector would be a better solution than a (static) array */ #define MAXEMPLOYEES 130 using namespace std; /* BEGIN Structs */ struct employee { string Name; int ID; char Salaried; float PayRate; string Department; string Title; }; /* END Structs */ /* BEGIN Function Prototypes */ int main(void); void menuStart(void); int menuChoice(void); int readEmployeeFile(employee personnel[]); void displayEmployeeByElement(employee personnel[], int& element); void searchByID(employee personnel[], int& numEmployees); void displayEmployeesAboveSalary(employee personnel[], int& numEmployees); void displayByGroupedDepartments(employee personnel[], int& numEmployees); void displayByAscendingID(employee personnel[], int& numEmployees); void displayPayroll_Info(employee personnel[], int& numEmployees); void displayByTitle(employee personnel[], int& numEmployees); int addNewEmployee(employee personnel[], int& numEmployees); void quitAndPrint(employee personnel[], int& numEmployees); /* END Function Prototypes */ /** * Main function */ int main() { menuStart(); } /** * Starts the menu while loop and switches based on the user's choice */ void menuStart(void) { /* Variable declarations */ int choice, numEmployees; struct employee personnel[MAXEMPLOYEES]; numEmployees = readEmployeeFile(personnel); /* Menu while and switch statements */ while(choice != 8) { choice = menuChoice(); switch(choice) { case 1: searchByID(personnel, numEmployees); break; case 2: displayEmployeesAboveSalary(personnel, numEmployees); break; case 3: displayByGroupedDepartments(personnel, numEmployees); break; case 4: displayByAscendingID(personnel, numEmployees); break; case 5: displayPayroll_Info(personnel, numEmployees); break; case 6: displayByTitle(personnel, numEmployees); break; case 7: numEmployees = addNewEmployee(personnel, numEmployees); break; case 8: break; default: cout << "Invalid choice; please choose an option from the menu." << endl << endl; break; } } /* only occurs when choice == 8 */ quitAndPrint(personnel, numEmployees); } /** * Displays the menu for the program * * @return choice the user's chosen menu item */ int menuChoice(void) { int choice; cout << setw(80) << setfill('*') << "" << endl; cout << "1. Search for an employee by ID number" << endl; cout << "2. Display employees above a certain salary" << endl; cout << "3. Display employees from a certain department" << endl; cout << "4. Display employee IDs and names in ascending order" << endl; cout << "5. Display payroll information" << endl; cout << "6. Display employees, by name, of a particular title" << endl; cout << "7. Add a new employee" << endl; cout << "8. Quit and Print" << endl << endl; cout << setw(80) << setfill('*') << "" << endl; cout << "Choose 1-8: "; cin >> choice; cout << endl; return choice; } /** * Reads data from "empdata.txt" into the personnel struct * * @param personnel[] personnel struct to read the employee data into * @return numEmployees number of employees read from the data file */ int readEmployeeFile(employee personnel[]) { /* opens file for input */ ifstream inFile; inFile.open("empdata.txt"); /* makes sure file is there */ if (!inFile) { cout << "Error: Cannot open \"empdata.txt\" for input." << endl; return -1; } /* reads data from the file and places it in the struct */ int numEmployees = 0; while(inFile.good()) { getline(inFile, personnel[numEmployees].Name); inFile >> personnel[numEmployees].ID; inFile >> personnel[numEmployees].Salaried; inFile >> personnel[numEmployees].PayRate; inFile.ignore(); getline(inFile, personnel[numEmployees].Department); getline(inFile, personnel[numEmployees].Title); numEmployees++; } /* returns the number of employees found in the file */ return numEmployees; } /** * Loops through the struct to find an employee matching a certain ID number * * @param personnel[] personnel struct on which to operate * @param numEmployees number of employees in the struct */ void searchByID(employee personnel[], int& numEmployees) { /* sort struct by ID */ int swapped = 1; for (int i = 0; (i <= (numEmployees - 1)) && swapped; i++) { swapped = 0; for (int j = 0; j < (numEmployees - 1); j++) { if (personnel[j].ID > personnel[j + 1].ID) { struct employee temp[1]; temp[0] = personnel[j]; personnel[j] = personnel[j + 1]; personnel[j + 1] = temp[0]; swapped = 1; } } } cout << "Enter the ID of the employee you wish to view: "; int requestedID; cin >> requestedID; cout << endl; /* loops through struct and displays Name, Department, Title */ for (int i = 0; i < numEmployees; i++) { if (personnel[i].ID == requestedID) { cout << "Name: " << personnel[i].Name << endl; cout << "Department: " << personnel[i].Department << endl; cout << "Title: " << personnel[i].Title << "\n\n"; return; } } cout << "No matching ID found.\n\n"; return; } /** * Loops through the struct to find employees above a certain salary level * * @param personnel[] personnel struct on which to operate * @param numEmployees number of employees in the struct */ void displayEmployeesAboveSalary(employee personnel[], int& numEmployees) { /* sort struct by PayRate */ int swapped = 1; for (int i = 0; (i <= (numEmployees - 1)) && swapped; i++) { swapped = 0; for (int j = 0; j < (numEmployees - 1); j++) { if (personnel[j].PayRate > personnel[j + 1].PayRate) { struct employee temp[1]; temp[0] = personnel[j]; personnel[j] = personnel[j + 1]; personnel[j + 1] = temp[0]; swapped = 1; } } } cout << "Enter the low bound of salaries to display: "; int lowestSalary; cin >> lowestSalary; cout << endl; /* loops through struct to check for the lowest matching salary level */ int lowestSalaryElement = -1; for (int i = 0; i < numEmployees; i++) { if (personnel[i].PayRate >= lowestSalary) { lowestSalaryElement = i; break; } } if (lowestSalaryElement == -1) { cout << "No salaries above " << lowestSalary << " found." << "\n\n"; return; } else { cout << "All employees earning at least " << lowestSalary << ": " << endl; /* loops through struct and displays Name */ for (int i = lowestSalaryElement; i < numEmployees; i++) { cout << personnel[i].Name << endl; } cout << endl; return; } } /** * Displays all employees who work in a certain department. * * @param personnel[] personnel struct on which to operate * @param numEmployees number of employees in the struct */ void displayByGroupedDepartments(employee personnel[], int& numEmployees) { /* sort struct by Department */ int swapped = 1; for (int i = 0; (i <= (numEmployees - 1)) && swapped; i++) { swapped = 0; for (int j = 0; j < (numEmployees - 1); j++) { if (personnel[j].Department > personnel[j + 1].Department) { struct employee temp[1]; temp[0] = personnel[j]; personnel[j] = personnel[j + 1]; personnel[j + 1] = temp[0]; swapped = 1; } } } cout << "Enter the Department you wish to view: "; string requestedDepartment; cin >> requestedDepartment; cout << endl; /* loops through struct to check for a match */ int matched = 0; for (int i = 0; i < numEmployees; i++) { if (personnel[i].Department == requestedDepartment) { matched = 1; break; } } if (matched) { cout << "Employees found in the " << requestedDepartment << " department:" << endl; /* loops through struct and displays Name */ for (int i = 0; i < numEmployees; i++) { if (personnel[i].Department == requestedDepartment) { cout << personnel[i].Name << endl; matched = 1; } } } else cout << "No employees found in the " << requestedDepartment << " department." << endl; cout << endl; return; } /** * Sorts employees in ascending order and displays each employee's name and ID number * * @param personnel[] personnel struct on which to operate * @param numEmployees number of employees in the struct */ void displayByAscendingID(employee personnel[], int& numEmployees) { /* sort struct by ID */ int swapped = 1; for (int i = 0; (i <= (numEmployees - 1)) && swapped; i++) { swapped = 0; for (int j = 0; j < (numEmployees - 1); j++) { if (personnel[j].ID > personnel[j + 1].ID) { struct employee temp[1]; temp[0] = personnel[j]; personnel[j] = personnel[j + 1]; personnel[j + 1] = temp[0]; swapped = 1; } } } cout << "\nEmployees listed by ID in ascending order:\n"; /* loops through struct and displays ID, Name */ for (int i = 0; i < numEmployees; i++) { printf("%3d - ",personnel[i].ID); cout << personnel[i].Name << endl; } cout << endl; return; } /** * Sorts employees in ascending order by ID number and displays names, * ID numbers, and bi-weekly pay... all grouped by department * * @param personnel[] personnel struct on which to operate * @param numEmployees number of employees in the struct */ void displayPayroll_Info(employee personnel[], int& numEmployees) { /* sort struct by ID */ int swapped = 1; for (int i = 0; (i <= (numEmployees - 1)) && swapped; i++) { swapped = 0; for (int j = 0; j < (numEmployees - 1); j++) { if (personnel[j].ID > personnel[j + 1].ID) { struct employee temp[1]; temp[0] = personnel[j]; personnel[j] = personnel[j + 1]; personnel[j + 1] = temp[0]; swapped = 1; } } } /* Lots of loops for the four departments to display ID, Name, PayRate */ cout << "Administration payroll:" << endl; for (int i = 0; i < numEmployees; i++) { if (personnel[i].Department == "Administration") { printf("%3d - ", personnel[i].ID); cout << personnel[i].Name << " earns " << personnel[i].PayRate << " bi-weekly.\n"; } } cout << "\nHR payroll:" << endl; for (int i = 0; i < numEmployees; i++) { if (personnel[i].Department == "HR") { printf("%3d - ", personnel[i].ID); cout << personnel[i].Name << " earns " << personnel[i].PayRate << " bi-weekly.\n"; } } cout << "\nMarketing payroll:" << endl; for (int i = 0; i < numEmployees; i++) { if (personnel[i].Department == "Marketing") { printf("%3d - ", personnel[i].ID); cout << personnel[i].Name << " earns " << personnel[i].PayRate << " bi-weekly.\n"; } } cout << "\nProduction payroll:" << endl; for (int i = 0; i < numEmployees; i++) { if (personnel[i].Department == "Production") { printf("%3d - ", personnel[i].ID); cout << personnel[i].Name << " earns " << personnel[i].PayRate << " bi-weekly.\n"; } } cout << endl; return; } /** * Displays all employees with a certain title * * @param personnel[] personnel struct on which to operate * @param numEmployees number of employees in the struct */ void displayByTitle(employee personnel[], int& numEmployees) { cout << "Enter the Title you wish to view: "; string requestedTitle; cin.ignore(30, '\n'); getline(cin, requestedTitle); /* loops through struct and checks for matches */ int matched = 0; for (int i = 0; i < numEmployees; i++) { if (personnel[i].Title == requestedTitle) { matched = 1; break; } } cout << endl; if (matched) { cout << "Employees matching the title \"" << requestedTitle << "\":\n"; /* loops through struct and displays Name */ for (int i = 0; i < numEmployees; i++) { if (personnel[i].Title == requestedTitle) { cout << personnel[i].Name << endl; } } } else cout << "No employees found with the title \"" << requestedTitle << "\".\n"; cout << endl; return; } /** * Prompts for the data of a new employee and adds it to the struct * * @param personnel[] personnel struct on which to operate * @param numEmployees number of employees in the struct */ int addNewEmployee(employee personnel[], int& numEmployees) { /* variables to prompt for */ string newName; int newID; char newSalaried; float newPayRate; string newDepartment; string newTitle; /* get data from user */ cout << " - New employee addition -" << endl; cout << "Name: "; cin.ignore(30, '\n'); getline(cin, newName); cout << "ID: "; cin >> newID; cout << "Salaried or Hourly ('s' or 'h'): "; cin >> newSalaried; cout << "Pay Rate: "; cin >> newPayRate; cout << "Department: "; cin.ignore(30, '\n'); getline(cin, newDepartment); cout << "Title: "; getline(cin, newTitle); /* place data into the struct */ personnel[numEmployees].Name = newName; personnel[numEmployees].ID = newID; personnel[numEmployees].Salaried = newSalaried; personnel[numEmployees].PayRate = newPayRate; personnel[numEmployees].Department = newDepartment; personnel[numEmployees].Title = newTitle; cout << endl << newName << " added.\n\n"; /* return the new value allowing for the newly added employee */ return (numEmployees + 1); } /** * Prints the data from the personnel struct to output file "empoutput.txt" * and quits the program * * @param personnel[] personnel struct on which to operate * @param numEmployees number of employees in the struct */ void quitAndPrint(employee personnel[], int& numEmployees) { /* sort struct by ID */ int swapped = 1; for (int i = 0; (i <= (numEmployees - 1)) && swapped; i++) { swapped = 0; for (int j = 0; j < (numEmployees - 1); j++) { if (personnel[j].ID > personnel[j + 1].ID) { struct employee temp[1]; temp[0] = personnel[j]; personnel[j] = personnel[j + 1]; personnel[j + 1] = temp[0]; swapped = 1; } } } /* opens file for output */ ofstream outFile; outFile.open("empoutput.txt"); // creates file if it doesn't exist /* makes sure file is there */ if (!outFile) { cout << "Error: Cannot open \"empoutput.txt\" for output." << endl; return; } /* outputs entirety of struct to the file */ for (int i = 0; i < numEmployees; i++) { outFile << personnel[i].Name << endl; outFile << personnel[i].ID << endl; outFile << personnel[i].Salaried << endl; outFile << personnel[i].PayRate << endl; outFile << personnel[i].Department << endl; outFile << personnel[i].Title << endl; } /* exits the program */ cout << "Personnel data output to \"empoutput.txt\"." << endl; system("pause"); exit(0); }