// Elaine Torrez #15 CS1A Chapter 11 – Multipurpose Payroll
/********************************************************************************************
*
* MULTIPURPOSE PAYROLL
*
* ------------------------------------------------------------------------------------------
* This program calculates pay for either:
*
* (1) an hourly paid worker OR
* (2) a salaried worker
*
* Two structures are used:
*
* HOURLY WORKER STRUCT:
* - HoursWorked (0–80)
* - HourlyRate (>= 0)
*
* SALARIED WORKER STRUCT:
* - Salary (>= 0)
* - Bonus (>= 0)
*
* A union is declared that contains *one member of each struct*. Only one type is used
* at a time, depending on user input.
*
* The program:
* 1. Asks the user which type of worker they want to calculate pay for.
* 2. Validates all numeric input (no negatives; HoursWorked ≤ 80).
* 3. Stores the appropriate values in the union.
* 4. Calculates and displays the total pay.
*
* ------------------------------------------------------------------------------------------
*
* INPUT
* selection : 1 = hourly worker, 2 = salaried worker
*
* IF hourly worker:
* HoursWorked (0–80)
* HourlyRate (>= 0)
*
* IF salaried worker:
* Salary (>= 0)
* Bonus (>= 0)
*
* OUTPUT
* Total pay for the selected worker type.
*
********************************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
//------------------------------------------------------------
// Structures
//------------------------------------------------------------
struct Hourly {
double HoursWorked;
double HourlyRate;
};
struct Salaried {
double Salary;
double Bonus;
};
//------------------------------------------------------------
// Union containing one hourly worker OR one salaried worker
//------------------------------------------------------------
union Worker {
Hourly hourly;
Salaried salaried;
};
int main()
{
Worker employee; // Union variable
int choice; // 1 = hourly, 2 = salaried
double totalPay = 0; // Result
cout << fixed << setprecision(2);
//--------------------------------------------------------
// Ask user which worker type
//--------------------------------------------------------
cout << "Payroll Calculator\n";
cout << "------------------\n";
cout << "1. Hourly Worker\n";
cout << "2. Salaried Worker\n";
cout << "Enter choice (1 or 2): ";
cin >> choice;
while (choice != 1 && choice != 2) {
cout << " **ERROR** Enter 1 or 2: ";
cin >> choice;
}
//--------------------------------------------------------
// HOURLY WORKER PROCESSING
//--------------------------------------------------------
if (choice == 1) {
cout << "\nEnter hours worked (0–80): ";
cin >> employee.hourly.HoursWorked;
while (employee.hourly.HoursWorked < 0 || employee.hourly.HoursWorked > 80) {
cout << " **ERROR** Hours must be 0–80. Re-enter: ";
cin >> employee.hourly.HoursWorked;
}
cout << "Enter hourly rate: ";
cin >> employee.hourly.HourlyRate;
while (employee.hourly.HourlyRate < 0) {
cout << " **ERROR** Hourly rate cannot be negative. Re-enter: ";
cin >> employee.hourly.HourlyRate;
}
// Calculate pay
totalPay = employee.hourly.HoursWorked * employee.hourly.HourlyRate;
}
//--------------------------------------------------------
// SALARIED WORKER PROCESSING
//--------------------------------------------------------
else {
cout << "\nEnter salary amount: ";
cin >> employee.salaried.Salary;
while (employee.salaried.Salary < 0) {
cout << " **ERROR** Salary cannot be negative. Re-enter: ";
cin >> employee.salaried.Salary;
}
cout << "Enter bonus amount: ";
cin >> employee.salaried.Bonus;
while (employee.salaried.Bonus < 0) {
cout << " **ERROR** Bonus cannot be negative. Re-enter: ";
cin >> employee.salaried.Bonus;
}
// Calculate pay
totalPay = employee.salaried.Salary + employee.salaried.Bonus;
}
//--------------------------------------------------------
// Final Output
//--------------------------------------------------------
cout << "\nTotal Pay: $" << totalPay << endl;
return 0;
}