SHARE
TWEET

Untitled

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