//Charlotte Davies-Kiernan CS1A Chapter 11 P. 650 #15
//
/******************************************************************************
*
* Compute Total Pay
* ____________________________________________________________________________
* This program will calculate the total pay of a worker depending on if they
* are a hourly worker or a salaried worker. If the worker is hourly the hours
* they worked will be inputted (between 0-80) and the hourly pay to calclate
* the total pay. If the worker is salary the salary and the bonus will need to
* be inputted to calculate the total pay.
* ____________________________________________________________________________
* Input
* WorkerType :Type of worker whether salary or hourly
* HoursWorked :Amount of hours the hourly worker worked
* HourlyRate :Pay per hour for the hourly worker
* Salary :Salary pay for salaried worker
* Bonus :Bonus pay for salaried worker
* Output
* totalPay :Total pay whether the worker is hourly or salary
*****************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
//Structures
struct Hourly {
float HoursWorked;
float HourlyRate;
};
struct Salaried {
float Salary;
float Bonus;
};
//Union for storing either worker type
union PayInfo {
Hourly hourly;
Salaried salaried;
};
//Function Prototypes
float getPositiveFloat(const string &prompt);
float getHoursWorked();
int getWorkerType();
float calculateHourlyPay(const Hourly &h);
float calculateSalariedPay(const Salaried &s);
int main() {
//Data Dictionary
PayInfo employee;
int choice = getWorkerType();
float totalPay = 0.0;
if(choice == 1) {
cout << "Hourly Paid Worker: " << endl;
employee.hourly.HoursWorked = getHoursWorked();
employee.hourly.HourlyRate = getPositiveFloat("Enter hourly rate: ");
totalPay = calculateHourlyPay(employee.hourly);
}
else {
cout << "Salaried Worker: " << endl;
employee.salaried.Salary = getPositiveFloat("Enter Salary: ");
employee.salaried.Bonus = getPositiveFloat("Enter bonus: ");
totalPay = calculateSalariedPay(employee.salaried);
}
cout << "\nTotal Pay: $" << totalPay << endl;
return 0;
}
//Function Defintions
float getPositiveFloat(const string &prompt) {
float value;
while (true) {
cout << prompt;
cin >> value;
if(!cin.fail () && value >= 0)
return value;
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid input. Please enter a non-negative number." << endl;
}
}
float getHoursWorked(){
float hours;
while(true) {
cout << "Enter hours worked (0 - 80): " << endl;
cin >> hours;
if(!cin.fail() && hours >= 0 && hours <= 80)
return hours;
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid input. Hours must be between 0 and 80." << endl;
}
}
int getWorkerType(){
int type;
while (true) {
cout << "Calculate pay for: " << endl;
cout << "1. Hourly Paid Worker " << endl;
cout << "2. Salaried Worker " << endl;
cout << "Enter choice: " << endl;
cin >> type;
if(!cin.fail() && (type == 1 || type == 2))
return type;
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid choice. Enter 1 or 2." << endl;
}
}
float calculateHourlyPay(const Hourly &h) {
return h.HoursWorked * h.HourlyRate;
}
float calculateSalariedPay(const Salaried &s){
return s.Salary + s.Bonus;
}