Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 5.92 KB | None | 0 0
  1. //Page 68-69 Exercise 2
  2. //Date: 6/19/2019
  3. //Programmed by: Liam Maiorino
  4. //Description: This program simulates a application that allows you to keep track
  5. //of employees with various types of information, allows you to calculate the wages
  6. //of those employees, search between them, and sort between them
  7.  
  8. #include <iostream.h>
  9. #include "apstring.h"
  10. #include "apvector.h"
  11.  
  12. //Declares Employee as a structure with 4 values declared inside to be used when called
  13. //upon
  14. struct Employee
  15. {
  16.     apstring name;
  17.     double hourlyRate;
  18.     int hoursWorked;
  19.     double wages;
  20. };
  21.  
  22. //Declares the functions used in the program
  23. int CalculateWages(apvector<Employee> &employeeList, int numEmployees);
  24. void ReadEmployee(Employee &person);
  25. void DisplayEmployee(const Employee &person);
  26. bool SearchList(const apvector<Employee> &employeeList, apstring searchName, int&foundLocation);
  27. void SortList(apvector<Employee> &employeeList, int numEmployees);
  28. bool isError(double input);
  29.  
  30.  
  31. int CalculateWages(apvector<Employee> &employeeList, int numEmployees)
  32. {
  33.     //Declares totalWages as a integer and makes it equal to 0, this is used
  34.     //to keep track of the total wages in the company
  35.     int totalWages=0;
  36.    
  37.     //Goes through each employee, calculates their wage, assigns that value to their
  38.     //wage value, and adds their value to the totalWages value.
  39.     for (int i=0; i < numEmployees; i++)
  40.     {
  41.         employeeList[i].wages = employeeList[i].hourlyRate * employeeList[i].hoursWorked;
  42.        
  43.         totalWages = totalWages + employeeList[i].wages;
  44.     }
  45.    
  46.     //Returns the total wage value to be outputted
  47.     return totalWages;
  48. }
  49.  
  50. bool isError(double input)
  51. {
  52.     //If the input is not a double then clear and ignore the input, alert the user of the error
  53.     //and return true. A double is used as a integer value is normally a double but a double
  54.     //is not a integer
  55.     if (!cin)
  56.     {
  57.         cin.clear();
  58.         cin.ignore(10000, '\n');
  59.         cout << "ERROR value not accepted, please check input and try again\n";
  60.         return true;
  61.     }
  62.     //Else if the input is less then 0 then output the error and return true
  63.     else if (input < 0)
  64.     {
  65.         cout << "ERROR value can not be negitive, please try again\n";
  66.         return true;
  67.     }
  68.     //Otherwise return false
  69.     else
  70.         return false;
  71. }
  72. void ReadEmployee(Employee &person)
  73. {
  74.     //Request the user to enter the current employees name
  75.     cout << "Please enter the employee's name: ";
  76.     cin >> person.name;
  77.    
  78.     //While the error function returns true request the user to enter
  79.     //a value for the employees hourly rate
  80.     do
  81.     {
  82.     cout << "Please enter " << person.name << "'s hourly rate: ";
  83.     cin >> person.hourlyRate;
  84.     } while(isError(person.hourlyRate));
  85.    
  86.     //While the error function returns true request the user to enter
  87.     //a value for the employees hours worked   
  88.     do
  89.     {
  90.     cout << "Please enter " << person.name << "'s hours worked: ";
  91.     cin >> person.hoursWorked;
  92.     } while(isError(person.hoursWorked));
  93. }
  94.  
  95.  
  96.  
  97. void DisplayEmployee(const Employee &person)
  98. {
  99.     //Output the desired employees information, including their name, hourlyRate, hours Worked
  100.     //and wage if calcualted
  101.     cout << "\nDisplaying Information For Employee " << person.name;
  102.     cout << endl << "---------------------------------------------";
  103.     cout << "\nHourly Rate: " << person.hourlyRate;
  104.     cout << "\nHours Worked: " << person.hoursWorked;
  105.     cout << "\nWage: " << person.wages;
  106. }
  107.  
  108. bool SearchList(const apvector<Employee> &employeeList, apstring searchName, int&foundLocation)
  109. {
  110.     cout << "test4";
  111. }
  112.  
  113. void SortList(apvector<Employee> &employeeList, int numEmployees)
  114. {
  115.     cout << "test5";
  116. }
  117.  
  118. int main()
  119. {
  120.     //Declares company as a vector with the type of the Employee structure
  121.     apvector<Employee> company;
  122.    
  123.     //Declares choice, numEmployees, and selectedEmployee as integer with starting
  124.     //values of 0
  125.     int choice=0, numEmployees=0, selectedEmployee=0;
  126.    
  127.     //While the choice value is no 0 (the quit value) request the user to enter
  128.     //a function and then preform the desired task if applicable
  129.     do
  130.     {
  131.         //Output the possible options
  132.         cout << "Gromit Industries Payroll" << endl;
  133.         cout << "-------------------------" << endl;
  134.         cout << "1. Enter new employee." << endl;
  135.         cout << "2. Display employee details." << endl;
  136.         cout << "3. Calculate wages." << endl;
  137.         cout << "4. Sort employees by wages." << endl;
  138.         cout << "5. Search by name." << endl;
  139.         cout << "0. Quit" << endl << endl;
  140.         cout << "Please make a selection: ";
  141.        
  142.         //Request the user to enter a option value while the error function returns true
  143.         do
  144.         {
  145.         cin >> choice;
  146.         } while (isError(choice));
  147.        
  148.         switch(choice)
  149.         {
  150.             case 0:
  151.             break;
  152.             case 1:
  153.                 //Adds 1 to the numEmployees value, resizes the vector for the new employee
  154.                 //calls on the readEmployee function to allow the user to enter values for the current
  155.                 //employee, then when finished output the result
  156.                 numEmployees++;
  157.                 company.resize(numEmployees);
  158.                 ReadEmployee(company[numEmployees-1]);
  159.                 cout << "New employee added successfully" << endl;
  160.             break;
  161.             case 2:
  162.                 //If there have been no employees added (aka numEmployees=0) alert the user
  163.                 //of this, otherwise call on the displayEmployee function in relation to the current
  164.                 //selected employee
  165.                 if (numEmployees==0)
  166.                 {
  167.                     cout << "\nNo employees detected, please try again";
  168.                 }
  169.                 else
  170.                 {
  171.                     DisplayEmployee(company[selectedEmployee]);
  172.                 }
  173.                
  174.             break;
  175.             case 3:
  176.                 //Output the totals wages by calling on the calculateWages function to return the
  177.                 //proper value
  178.                 cout << "\nTotal Wages is equal to $" << CalculateWages(company, numEmployees);
  179.             break;
  180.             case 4:
  181.                 SortList(company, numEmployees);
  182.             break;
  183.             case 5:
  184.                
  185.             break;
  186.            
  187.             default:
  188.                 //Alert the user that the only available options are 1-5
  189.                 cout << endl << "Invalid Option. Between 1 and 5 only" << endl;
  190.         }
  191.        
  192.         //Output lines for spacing
  193.         cout << endl << endl;
  194.        
  195.     } while (choice!=0);
  196.    
  197.     //Return 0 to prevent errors
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement