fork download
  1. // Elaine Torrez #15 CS1A Chapter 11 – Multipurpose Payroll
  2.  
  3. /********************************************************************************************
  4.  *
  5.  * MULTIPURPOSE PAYROLL
  6.  *
  7.  * ------------------------------------------------------------------------------------------
  8.  * This program calculates pay for either:
  9.  *
  10.  * (1) an hourly paid worker OR
  11.  * (2) a salaried worker
  12.  *
  13.  * Two structures are used:
  14.  *
  15.  * HOURLY WORKER STRUCT:
  16.  * - HoursWorked (0–80)
  17.  * - HourlyRate (>= 0)
  18.  *
  19.  * SALARIED WORKER STRUCT:
  20.  * - Salary (>= 0)
  21.  * - Bonus (>= 0)
  22.  *
  23.  * A union is declared that contains *one member of each struct*. Only one type is used
  24.  * at a time, depending on user input.
  25.  *
  26.  * The program:
  27.  * 1. Asks the user which type of worker they want to calculate pay for.
  28.  * 2. Validates all numeric input (no negatives; HoursWorked ≤ 80).
  29.  * 3. Stores the appropriate values in the union.
  30.  * 4. Calculates and displays the total pay.
  31.  *
  32.  * ------------------------------------------------------------------------------------------
  33.  *
  34.  * INPUT
  35.  * selection : 1 = hourly worker, 2 = salaried worker
  36.  *
  37.  * IF hourly worker:
  38.  * HoursWorked (0–80)
  39.  * HourlyRate (>= 0)
  40.  *
  41.  * IF salaried worker:
  42.  * Salary (>= 0)
  43.  * Bonus (>= 0)
  44.  *
  45.  * OUTPUT
  46.  * Total pay for the selected worker type.
  47.  *
  48.  ********************************************************************************************/
  49.  
  50. #include <iostream>
  51. #include <iomanip>
  52. using namespace std;
  53.  
  54. //------------------------------------------------------------
  55. // Structures
  56. //------------------------------------------------------------
  57. struct Hourly {
  58. double HoursWorked;
  59. double HourlyRate;
  60. };
  61.  
  62. struct Salaried {
  63. double Salary;
  64. double Bonus;
  65. };
  66.  
  67. //------------------------------------------------------------
  68. // Union containing one hourly worker OR one salaried worker
  69. //------------------------------------------------------------
  70. union Worker {
  71. Hourly hourly;
  72. Salaried salaried;
  73. };
  74.  
  75. int main()
  76. {
  77. Worker employee; // Union variable
  78. int choice; // 1 = hourly, 2 = salaried
  79. double totalPay = 0; // Result
  80.  
  81. cout << fixed << setprecision(2);
  82.  
  83. //--------------------------------------------------------
  84. // Ask user which worker type
  85. //--------------------------------------------------------
  86. cout << "Payroll Calculator\n";
  87. cout << "------------------\n";
  88. cout << "1. Hourly Worker\n";
  89. cout << "2. Salaried Worker\n";
  90. cout << "Enter choice (1 or 2): ";
  91. cin >> choice;
  92.  
  93. while (choice != 1 && choice != 2) {
  94. cout << " **ERROR** Enter 1 or 2: ";
  95. cin >> choice;
  96. }
  97.  
  98. //--------------------------------------------------------
  99. // HOURLY WORKER PROCESSING
  100. //--------------------------------------------------------
  101. if (choice == 1) {
  102. cout << "\nEnter hours worked (0–80): ";
  103. cin >> employee.hourly.HoursWorked;
  104.  
  105. while (employee.hourly.HoursWorked < 0 || employee.hourly.HoursWorked > 80) {
  106. cout << " **ERROR** Hours must be 0–80. Re-enter: ";
  107. cin >> employee.hourly.HoursWorked;
  108. }
  109.  
  110. cout << "Enter hourly rate: ";
  111. cin >> employee.hourly.HourlyRate;
  112.  
  113. while (employee.hourly.HourlyRate < 0) {
  114. cout << " **ERROR** Hourly rate cannot be negative. Re-enter: ";
  115. cin >> employee.hourly.HourlyRate;
  116. }
  117.  
  118. // Calculate pay
  119. totalPay = employee.hourly.HoursWorked * employee.hourly.HourlyRate;
  120. }
  121.  
  122. //--------------------------------------------------------
  123. // SALARIED WORKER PROCESSING
  124. //--------------------------------------------------------
  125. else {
  126. cout << "\nEnter salary amount: ";
  127. cin >> employee.salaried.Salary;
  128.  
  129. while (employee.salaried.Salary < 0) {
  130. cout << " **ERROR** Salary cannot be negative. Re-enter: ";
  131. cin >> employee.salaried.Salary;
  132. }
  133.  
  134. cout << "Enter bonus amount: ";
  135. cin >> employee.salaried.Bonus;
  136.  
  137. while (employee.salaried.Bonus < 0) {
  138. cout << " **ERROR** Bonus cannot be negative. Re-enter: ";
  139. cin >> employee.salaried.Bonus;
  140. }
  141.  
  142. // Calculate pay
  143. totalPay = employee.salaried.Salary + employee.salaried.Bonus;
  144. }
  145.  
  146. //--------------------------------------------------------
  147. // Final Output
  148. //--------------------------------------------------------
  149. cout << "\nTotal Pay: $" << totalPay << endl;
  150.  
  151. return 0;
  152. }
  153.  
Success #stdin #stdout 0s 5308KB
stdin
1
40
20
stdout
Payroll Calculator
------------------
1. Hourly Worker
2. Salaried Worker
Enter choice (1 or 2): 
Enter hours worked (0–80): Enter hourly rate: 
Total Pay: $800.00