Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Page 68-69 Exercise 2
- //Date: 6/19/2019
- //Programmed by: Liam Maiorino
- //Description: This program simulates a application that allows you to keep track
- //of employees with various types of information, allows you to calculate the wages
- //of those employees, search between them, and sort between them
- #include <iostream.h>
- #include "apstring.h"
- #include "apvector.h"
- //Declares Employee as a structure with 4 values declared inside to be used when called
- //upon
- struct Employee
- {
- apstring name;
- double hourlyRate;
- int hoursWorked;
- double wages;
- };
- //Declares the functions used in the program
- int CalculateWages(apvector<Employee> &employeeList, int numEmployees);
- void ReadEmployee(Employee &person);
- void DisplayEmployee(const Employee &person);
- bool SearchList(const apvector<Employee> &employeeList, apstring searchName, int&foundLocation);
- void SortList(apvector<Employee> &employeeList, int numEmployees);
- bool isError(double input);
- int CalculateWages(apvector<Employee> &employeeList, int numEmployees)
- {
- //Declares totalWages as a integer and makes it equal to 0, this is used
- //to keep track of the total wages in the company
- int totalWages=0;
- //Goes through each employee, calculates their wage, assigns that value to their
- //wage value, and adds their value to the totalWages value.
- for (int i=0; i < numEmployees; i++)
- {
- employeeList[i].wages = employeeList[i].hourlyRate * employeeList[i].hoursWorked;
- totalWages = totalWages + employeeList[i].wages;
- }
- //Returns the total wage value to be outputted
- return totalWages;
- }
- bool isError(double input)
- {
- //If the input is not a double then clear and ignore the input, alert the user of the error
- //and return true. A double is used as a integer value is normally a double but a double
- //is not a integer
- if (!cin)
- {
- cin.clear();
- cin.ignore(10000, '\n');
- cout << "ERROR value not accepted, please check input and try again\n";
- return true;
- }
- //Else if the input is less then 0 then output the error and return true
- else if (input < 0)
- {
- cout << "ERROR value can not be negitive, please try again\n";
- return true;
- }
- //Otherwise return false
- else
- return false;
- }
- void ReadEmployee(Employee &person)
- {
- //Request the user to enter the current employees name
- cout << "Please enter the employee's name: ";
- cin >> person.name;
- //While the error function returns true request the user to enter
- //a value for the employees hourly rate
- do
- {
- cout << "Please enter " << person.name << "'s hourly rate: ";
- cin >> person.hourlyRate;
- } while(isError(person.hourlyRate));
- //While the error function returns true request the user to enter
- //a value for the employees hours worked
- do
- {
- cout << "Please enter " << person.name << "'s hours worked: ";
- cin >> person.hoursWorked;
- } while(isError(person.hoursWorked));
- }
- void DisplayEmployee(const Employee &person)
- {
- //Output the desired employees information, including their name, hourlyRate, hours Worked
- //and wage if calcualted
- cout << "\nDisplaying Information For Employee " << person.name;
- cout << endl << "---------------------------------------------";
- cout << "\nHourly Rate: " << person.hourlyRate;
- cout << "\nHours Worked: " << person.hoursWorked;
- cout << "\nWage: " << person.wages;
- }
- bool SearchList(const apvector<Employee> &employeeList, apstring searchName, int&foundLocation)
- {
- cout << "test4";
- }
- void SortList(apvector<Employee> &employeeList, int numEmployees)
- {
- cout << "test5";
- }
- int main()
- {
- //Declares company as a vector with the type of the Employee structure
- apvector<Employee> company;
- //Declares choice, numEmployees, and selectedEmployee as integer with starting
- //values of 0
- int choice=0, numEmployees=0, selectedEmployee=0;
- //While the choice value is no 0 (the quit value) request the user to enter
- //a function and then preform the desired task if applicable
- do
- {
- //Output the possible options
- cout << "Gromit Industries Payroll" << endl;
- cout << "-------------------------" << endl;
- cout << "1. Enter new employee." << endl;
- cout << "2. Display employee details." << endl;
- cout << "3. Calculate wages." << endl;
- cout << "4. Sort employees by wages." << endl;
- cout << "5. Search by name." << endl;
- cout << "0. Quit" << endl << endl;
- cout << "Please make a selection: ";
- //Request the user to enter a option value while the error function returns true
- do
- {
- cin >> choice;
- } while (isError(choice));
- switch(choice)
- {
- case 0:
- break;
- case 1:
- //Adds 1 to the numEmployees value, resizes the vector for the new employee
- //calls on the readEmployee function to allow the user to enter values for the current
- //employee, then when finished output the result
- numEmployees++;
- company.resize(numEmployees);
- ReadEmployee(company[numEmployees-1]);
- cout << "New employee added successfully" << endl;
- break;
- case 2:
- //If there have been no employees added (aka numEmployees=0) alert the user
- //of this, otherwise call on the displayEmployee function in relation to the current
- //selected employee
- if (numEmployees==0)
- {
- cout << "\nNo employees detected, please try again";
- }
- else
- {
- DisplayEmployee(company[selectedEmployee]);
- }
- break;
- case 3:
- //Output the totals wages by calling on the calculateWages function to return the
- //proper value
- cout << "\nTotal Wages is equal to $" << CalculateWages(company, numEmployees);
- break;
- case 4:
- SortList(company, numEmployees);
- break;
- case 5:
- break;
- default:
- //Alert the user that the only available options are 1-5
- cout << endl << "Invalid Option. Between 1 and 5 only" << endl;
- }
- //Output lines for spacing
- cout << endl << endl;
- } while (choice!=0);
- //Return 0 to prevent errors
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement