Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.19 KB | None | 0 0
  1. // assign_problem6.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <conio.h>
  6. #include <string>
  7.  
  8. using namespace System;
  9. using namespace std;
  10.  
  11. //Prototypes
  12. //Sets up the screen for entering data
  13. void setScreenData(void);
  14. //Sets up the screen for displaying payments
  15. void setScreenPay(void);
  16. //Calculates worker's payments
  17. void calcWorkerPay(double payRate, int hours, double & basicPay, double & overtimePay, double & grossPay,
  18.                    double & hourlyRate, double & timeAndAHalf, double & doubleTime, double totals[]);
  19.  
  20. int main(array<System::String ^> ^args)
  21. {
  22.     //Declare variables
  23.     string name;
  24.     int hours;
  25.     double payRate = 5.8, basicPay = 0, overtimePay = 0, grossPay = 0, hourlyRate = 0, timeAndAHalf = 0,
  26.         doubleTime = 0, totals[3] = {0};
  27.     char again;
  28.  
  29.     //Allow to user to enter employee details as many times as they want
  30.     do
  31.     {
  32.         //Set up the screen
  33.         setScreenData();
  34.  
  35.         //Get the employee's name
  36.         Console::SetCursorPosition(41, 4);
  37.         getline(cin, name);
  38.  
  39.         //Get the employee's worked hours
  40.         Console::SetCursorPosition(41, 5);
  41.         cin >> hours;
  42.  
  43.         //Remove the last character (\n) from istream to avoid errors with getline
  44.         cin.ignore();
  45.  
  46.         //Calculate the employee's pay
  47.         calcWorkerPay(payRate, hours, basicPay, overtimePay, grossPay, hourlyRate, timeAndAHalf,
  48.             doubleTime, totals);
  49.  
  50.         //Output the results
  51.         Console::SetCursorPosition(41, 10); cout << basicPay;
  52.         Console::SetCursorPosition(41, 12); cout << overtimePay;
  53.         Console::SetCursorPosition(41, 14); cout << grossPay;
  54.  
  55.         //Ask the user if they would like to move on to the next employee
  56.         Console::SetCursorPosition(20, 24);
  57.         cout << "Next employee? (Y)es / (N)o [_]";
  58.  
  59.         //Get the user's reply
  60.         Console::SetCursorPosition(49, 24);
  61.         again = getche();
  62.  
  63.         //Convert again to lowercase
  64.         again = tolower(again);
  65.  
  66.         //Show an error message and get another character from the user if they
  67.         //enter an invalid option
  68.         while(again != 'n' && again != 'y')
  69.         {
  70.             //Show the error message
  71.             Console::SetCursorPosition(60, 24);
  72.             cout << "Invalid option!";
  73.  
  74.             //Remove the character the user entered
  75.             Console::SetCursorPosition(49, 24);
  76.             cout << " ]";
  77.  
  78.             //Get another character from the user
  79.             Console::SetCursorPosition(49, 24);
  80.             again = getche();
  81.  
  82.             //Convert again to lowercase
  83.             again = tolower(again);
  84.  
  85.             //Remove the error message
  86.             Console::SetCursorPosition(60, 24);
  87.             cout << "               ";
  88.         }
  89.     }while(again != 'n');
  90.  
  91.     //Set up the screen
  92.     setScreenPay();
  93.  
  94.     //Output the totals
  95.     Console::SetCursorPosition(41, 6); cout << totals[0];
  96.     Console::SetCursorPosition(41, 8); cout << totals[1];
  97.     Console::SetCursorPosition(41, 10); cout << totals[2];
  98.  
  99.     //Wait for a key press before exiting
  100.     getch();
  101. }
  102.  
  103. void setScreenData(void)
  104. {
  105.     //Clear the screen
  106.     Console::Clear();
  107.  
  108.     //Display layout
  109.     Console::SetCursorPosition(30, 2); cout << "Worker Payments";
  110.  
  111.     Console::SetCursorPosition(10, 4); cout << "Name:";
  112.     Console::SetCursorPosition(10, 5); cout << "Hours worked:";
  113.  
  114.     Console::SetCursorPosition(10, 10); cout << "Basic pay:";
  115.     Console::SetCursorPosition(10, 12); cout << "Overtime pay:";
  116.     Console::SetCursorPosition(10, 14); cout << "Gross pay:";
  117.  
  118.     Console::SetCursorPosition(40, 4); cout << "[                       ]";
  119.     Console::SetCursorPosition(40, 5); cout << "[   ]";
  120.  
  121.     Console::SetCursorPosition(40, 10); cout << "[        ]";
  122.     Console::SetCursorPosition(40, 12); cout << "[        ]";
  123.     Console::SetCursorPosition(40, 14); cout << "[        ]";
  124. }
  125.  
  126. void setScreenPay(void)
  127. {
  128.     //Clear the screen
  129.     Console::Clear();
  130.  
  131.     //Display layout
  132.     Console::SetCursorPosition(30, 2); cout << "Total payments";
  133.  
  134.     Console::SetCursorPosition(10, 4); cout << "Rate of pay:";
  135.     Console::SetCursorPosition(40, 4); cout << "Total:";
  136.  
  137.     Console::SetCursorPosition(10, 6); cout << "Hourly rate";
  138.     Console::SetCursorPosition(10, 8); cout << "Time and a half";
  139.     Console::SetCursorPosition(10, 10); cout << "Double time";
  140.  
  141.     Console::SetCursorPosition(40, 6); cout << "[        ]";
  142.     Console::SetCursorPosition(40, 8); cout << "[        ]";
  143.     Console::SetCursorPosition(40, 10); cout << "[        ]";
  144.  
  145.     Console::SetCursorPosition(30, 24);
  146.     cout << "Press any key to exit...";
  147. }
  148.  
  149. void calcWorkerPay(double payRate, int hours, double & basicPay, double & overtimePay, double & grossPay,
  150.                    double & hourlyRate, double & timeAndAHalf, double & doubleTime, double totals[])
  151. {
  152.     //Calculate the amount of pay for each category of pay rates
  153.     if(hours > 60)
  154.     {
  155.         //Normal pay
  156.         hourlyRate = 35 * payRate;
  157.  
  158.         //Time and a half
  159.         timeAndAHalf = 25 * (payRate * 1.5);
  160.  
  161.         //Double time
  162.         doubleTime = (hours - 60) * (payRate * 2);
  163.     }
  164.     else
  165.     {
  166.         if(hours > 35)
  167.         {
  168.             //Normal pay
  169.             hourlyRate = 35 * payRate;
  170.  
  171.             //Time and a half
  172.             timeAndAHalf = (hours - 35) * (payRate * 1.5);
  173.         }
  174.         else
  175.             //Normal pay
  176.             hourlyRate = hours * payRate;
  177.     }
  178.  
  179.     //Calcuate basic pay
  180.     basicPay = hourlyRate;
  181.  
  182.     //Calculate overtime pay
  183.     overtimePay = timeAndAHalf + doubleTime;
  184.  
  185.     //Calculate gross pay
  186.     grossPay = basicPay + overtimePay;
  187.  
  188.     //Add worker payments to totals
  189.     totals[0] += hourlyRate;
  190.     totals[1] += timeAndAHalf;
  191.     totals[2] += doubleTime;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement