Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.76 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, bool isWagesCalculated);
  26. bool SearchList(const apvector<Employee> &employeeList, apstring &searchName, int&foundLocation, int numEmployees);
  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, bool isWagesCalculated)
  98. {
  99.     //Output the desired employees information, including their name, hourlyRate, and hours Worked
  100.     cout << "\nDisplaying Information For Employee " << person.name;
  101.     cout << endl << "---------------------------------------------";
  102.     cout << "\nHourly Rate: $" << person.hourlyRate << "/hr";
  103.     cout << "\nHours Worked: " << person.hoursWorked << "hrs";
  104.    
  105.     //If the wagesCalculated variable is true then output the wage result, otherwise
  106.     //output that the user must calculate the wages first then try again.
  107.     if (isWagesCalculated)
  108.         cout << "\nWage: $" << person.wages;
  109.     else
  110.         cout << "\nWage: Please Calculate Wages And Try Again";
  111. }
  112.  
  113. bool SearchList(const apvector<Employee> &employeeList, apstring &searchName, int&foundLocation, int numEmployees)
  114. {  
  115.     //Declares found to check if the value has been found or not
  116.     bool found=0;
  117.    
  118.     //Declares scan as a integer, scan is given a starting value of 0 and is
  119.     //used to be increased by 1 everytime we scan the vector for the value we are looking for
  120.     int scan=0;
  121.    
  122.     //Clears and ignore previous input in the cin
  123.     cin.clear();
  124.     cin.ignore(10000, '\n');
  125.    
  126.     //Request the user to enter a value for name they are requesting to search for
  127.     cout << "\nWhat value would you like to search for [VALUES ARE CASE SENSITIVE]: ";
  128.     cin >> searchName;
  129.     cout << endl;
  130.        
  131.     //While the found boolean equal to not true and the scan variable is less then or equal to
  132.     //the numeber of employees (since once we find the value we dont want to keep searching and
  133.     //once we've gone through the size of the board we dont want to keep searching) check if the
  134.     //current element of the vector is equal to the value we are looking for, if so then set the found
  135.     //boolean to true, the foundLocation variable to the current value of the vector that we are on
  136.     //and return true, otherwise add 1 to the scan variable.
  137.     while (not(found) && scan<numEmployees)
  138.     {
  139.         if (employeeList[scan].name==searchName)
  140.         {
  141.             found = true;
  142.             foundLocation = scan;
  143.             return true;
  144.         }
  145.         else
  146.             scan = scan+1;
  147.     }
  148.    
  149.     //return false as if the loop ends without returning true then we want to return false
  150.     return false;
  151. }
  152.  
  153. void SortList(apvector<Employee> &employeeList, int numEmployees)
  154. {
  155.     //temp and shuffle variables are declared as integers. these will be used to hold
  156.     //values for comparison
  157.     int temp;
  158.     int shuffle;
  159.    
  160.     //loops starting at 1 as we assume the first element is already sorted, it then
  161.     //loops through the vector until it reaches the size of the vector. In this loop
  162.     //it makes the temp value equal to the current loop wage value. Then a loop
  163.     //happens going through values of the board that require being swapped
  164.     for (int loop = 1; loop < numEmployees; loop++)
  165.     {
  166.         temp = employeeList[loop].wages;
  167.        
  168.         for (shuffle = loop; shuffle > 0 && temp < employeeList[shuffle - 1].wages; shuffle--)
  169.         {
  170.             std::swap(employeeList[shuffle], employeeList[shuffle-1]);
  171.             cout << "\n\nList Successfully Sorted.\n";
  172.         }
  173.     }
  174. }
  175.  
  176. int main()
  177. {
  178.     //Declares company as a vector with the type of the Employee structure
  179.     apvector<Employee> company;
  180.    
  181.     //Declares choice, numEmployees, and selectedEmployee as integer with starting
  182.     //values of 0
  183.     int choice=0, numEmployees=0, selectedEmployee=0;
  184.    
  185.     //Declares foundLocation as a integer, this variable is used to hold the element
  186.     //of the vector that the desired employee is located in
  187.     int foundLocation;
  188.    
  189.     bool isWagesCalculated=0;
  190.    
  191.     //While the choice value is no 0 (the quit value) request the user to enter
  192.     //a function and then preform the desired task if applicable
  193.     do
  194.     {
  195.         //Output the possible options
  196.         cout << "Gromit Industries Payroll" << endl;
  197.         cout << "-------------------------" << endl;
  198.         cout << "1. Enter new employee." << endl;
  199.         cout << "2. Display employee details." << endl;
  200.         cout << "3. Calculate wages." << endl;
  201.         cout << "4. Sort employees by wages." << endl;
  202.         cout << "5. Search by name." << endl;
  203.         cout << "0. Quit" << endl << endl;
  204.         cout << "Please make a selection: ";
  205.        
  206.         //Request the user to enter a option value while the error function returns true
  207.         do
  208.         {
  209.         cin >> choice;
  210.         } while (isError(choice));
  211.        
  212.         switch(choice)
  213.         {
  214.             case 0:
  215.             break;
  216.             case 1:
  217.                 //Adds 1 to the numEmployees value, resizes the vector for the new employee
  218.                 //calls on the readEmployee function to allow the user to enter values for the current
  219.                 //employee, then when finished output the result
  220.                 numEmployees++;
  221.                 company.resize(numEmployees);
  222.                 ReadEmployee(company[numEmployees-1]);
  223.                 cout << "New employee added successfully" << endl;
  224.                
  225.                 //sets isWagesCalculated to 0 as a new employee will be added with a uncalculated
  226.                 //wage, this is done to prevent outputting failed results
  227.                 isWagesCalculated = 0;
  228.             break;
  229.             case 2:
  230.                 //If there have been no employees added (aka numEmployees=0) alert the user
  231.                 //of this, otherwise call on the displayEmployee function in relation to the current
  232.                 //selected employee
  233.                 if (numEmployees==0)
  234.                 {
  235.                     cout << "\nNo employees detected, please try again";
  236.                 }
  237.                 else
  238.                 {
  239.                     DisplayEmployee(company[selectedEmployee], isWagesCalculated);
  240.                 }
  241.                
  242.             break;
  243.             case 3:
  244.                 //Output the totals wages by calling on the calculateWages function to return the
  245.                 //proper value and sets the isWagesCalculated value to true
  246.                 cout << "\nTotal Wages is equal to $" << CalculateWages(company, numEmployees);
  247.                 isWagesCalculated = 1;
  248.             break;
  249.             case 4:
  250.                 //Calls on the sortlist function to sort the employees by their wages
  251.                 SortList(company, numEmployees);
  252.                
  253.                 //Outputs the result of the sort
  254.                 cout << "List now sorted based on employee wages";
  255.             break;
  256.             case 5:
  257.                 //Declares searchName as a apstring to be used as the variable that the user
  258.                 //will input for what name they are searching for
  259.                 apstring searchName;
  260.                
  261.                 //if the searchList function returns true then output where the desired employee
  262.                 //was found and set the selectedEmployee variable to the location of the desired
  263.                 //employee in the vector. otherwise output that the desired employee was not found
  264.                 //in the vector
  265.                 if (SearchList(company, searchName, foundLocation, numEmployees))
  266.                 {
  267.                     cout << searchName << " was found at element " << foundLocation << ". Employee now selected";
  268.                     selectedEmployee = foundLocation;
  269.                 }
  270.                 else
  271.                 {
  272.                     cout << searchName << " was not found in the vector";
  273.                 }
  274.             break;
  275.            
  276.             default:
  277.                 //Alert the user that the only available options are 1-5
  278.                 cout << endl << "Invalid Option. Between 1 and 5 only" << endl;
  279.         }
  280.        
  281.         //Output lines for spacing
  282.         cout << endl << endl;
  283.        
  284.     } while (choice!=0);
  285.    
  286.     //Return 0 to prevent errors
  287.     return 0;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement