fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 11 P. 650 #15
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compute Total Pay
  6.  * ____________________________________________________________________________
  7.  * This program will calculate the total pay of a worker depending on if they
  8.  * are a hourly worker or a salaried worker. If the worker is hourly the hours
  9.  * they worked will be inputted (between 0-80) and the hourly pay to calclate
  10.  * the total pay. If the worker is salary the salary and the bonus will need to
  11.  * be inputted to calculate the total pay.
  12.  * ____________________________________________________________________________
  13.  * Input
  14.  * WorkerType :Type of worker whether salary or hourly
  15.  * HoursWorked :Amount of hours the hourly worker worked
  16.  * HourlyRate :Pay per hour for the hourly worker
  17.  * Salary :Salary pay for salaried worker
  18.  * Bonus :Bonus pay for salaried worker
  19.  * Output
  20.  * totalPay :Total pay whether the worker is hourly or salary
  21.  *****************************************************************************/
  22. #include <iostream>
  23. #include <iomanip>
  24. using namespace std;
  25.  
  26. //Structures
  27. struct Hourly {
  28. float HoursWorked;
  29. float HourlyRate;
  30. };
  31. struct Salaried {
  32. float Salary;
  33. float Bonus;
  34. };
  35.  
  36. //Union for storing either worker type
  37. union PayInfo {
  38. Hourly hourly;
  39. Salaried salaried;
  40. };
  41.  
  42. //Function Prototypes
  43. float getPositiveFloat(const string &prompt);
  44. float getHoursWorked();
  45. int getWorkerType();
  46. float calculateHourlyPay(const Hourly &h);
  47. float calculateSalariedPay(const Salaried &s);
  48.  
  49. int main() {
  50. //Data Dictionary
  51. PayInfo employee;
  52. int choice = getWorkerType();
  53. float totalPay = 0.0;
  54.  
  55. if(choice == 1) {
  56. cout << "Hourly Paid Worker: " << endl;
  57. employee.hourly.HoursWorked = getHoursWorked();
  58. employee.hourly.HourlyRate = getPositiveFloat("Enter hourly rate: ");
  59.  
  60. totalPay = calculateHourlyPay(employee.hourly);
  61. }
  62. else {
  63. cout << "Salaried Worker: " << endl;
  64. employee.salaried.Salary = getPositiveFloat("Enter Salary: ");
  65. employee.salaried.Bonus = getPositiveFloat("Enter bonus: ");
  66.  
  67. totalPay = calculateSalariedPay(employee.salaried);
  68. }
  69. cout << "\nTotal Pay: $" << totalPay << endl;
  70.  
  71. return 0;
  72. }
  73.  
  74. //Function Defintions
  75. float getPositiveFloat(const string &prompt) {
  76. float value;
  77. while (true) {
  78. cout << prompt;
  79. cin >> value;
  80.  
  81. if(!cin.fail () && value >= 0)
  82. return value;
  83.  
  84. cin.clear();
  85. cin.ignore(1000, '\n');
  86. cout << "Invalid input. Please enter a non-negative number." << endl;
  87. }
  88. }
  89.  
  90. float getHoursWorked(){
  91. float hours;
  92. while(true) {
  93. cout << "Enter hours worked (0 - 80): " << endl;
  94. cin >> hours;
  95.  
  96. if(!cin.fail() && hours >= 0 && hours <= 80)
  97. return hours;
  98.  
  99. cin.clear();
  100. cin.ignore(1000, '\n');
  101. cout << "Invalid input. Hours must be between 0 and 80." << endl;
  102. }
  103. }
  104.  
  105. int getWorkerType(){
  106. int type;
  107. while (true) {
  108. cout << "Calculate pay for: " << endl;
  109. cout << "1. Hourly Paid Worker " << endl;
  110. cout << "2. Salaried Worker " << endl;
  111. cout << "Enter choice: " << endl;
  112. cin >> type;
  113.  
  114. if(!cin.fail() && (type == 1 || type == 2))
  115. return type;
  116.  
  117. cin.clear();
  118. cin.ignore(1000, '\n');
  119. cout << "Invalid choice. Enter 1 or 2." << endl;
  120. }
  121. }
  122.  
  123. float calculateHourlyPay(const Hourly &h) {
  124. return h.HoursWorked * h.HourlyRate;
  125. }
  126.  
  127. float calculateSalariedPay(const Salaried &s){
  128. return s.Salary + s.Bonus;
  129. }
Success #stdin #stdout 0.01s 5320KB
stdin
3
1
46
23.50
stdout
Calculate pay for: 
1. Hourly Paid Worker 
2. Salaried Worker 
Enter choice: 
Invalid choice. Enter 1 or 2.
Calculate pay for: 
1. Hourly Paid Worker 
2. Salaried Worker 
Enter choice: 
Hourly Paid Worker: 
Enter hours worked (0 - 80): 
Enter hourly rate: 
Total Pay: $1081