In C++, Write a program that calculates pay for either an hourly paid worker or
ID: 3830985 • Letter: I
Question
In C++,
Write a program that calculates pay for either an hourly paid worker or a salaried worker. Hourly paid workers are paid their hourly pay rate times the number of hours worked. Salaried workers are paid their regular salary plus any bonus they may have earned. The program should declare two structures for the following data:
Hourly Paid:
HoursWorked
HourlyRate
Salaried:
Salary
Bonus
The program should also declare a union with two members. Each member should be a structure variable: one for the hourly paid worker and another for the salaried worker. The program should ask the user whether he or she is calculating the pay for an hourly paid worker or a salaried worker. Regardless of which the user selects, the appropriate members of the union will be used to store the data that will be used to calculate
the pay.
Input Validation: Do not accept negative numbers. Do not accept values greater than
80 for HoursWorked.
Explanation / Answer
#include <iostream>
using namespace std;
union Employee
{
struct
{
int hoursWorked;
double hourlyRate;
}hourlyPaid;;
struct
{
double salary;
double bonus;
}salaried;
};
int main()
{
union Employee emp;
int employeeType;
int hoursWorked;
double hoursWorked1,hourlyRate1,salary1,bonus1;
cout<<" Enter the type of Employee <1-salaried, 2-hourlyPaid>: ";
cin>>employeeType;
if(employeeType == 1)
{
cout<<" Enter salary : ";
cin>>salary1;
if(salary1 < 0)
{
cout<<" Salary cannot be negative.Enter again";
cin>>salary1;
}
emp.salaried.salary = salary1;
cout<<" Enter bonus : ";
cin>>bonus1;
if(bonus1 < 0)
{
cout<<" bonus cannot be negative.Enter again";
cin>>bonus1;
}
emp.salaried.bonus = bonus1;
cout<<" Total Pay = "<<(emp.salaried.salary + emp.salaried.bonus);
}
else if(employeeType == 2)
{
cout<<" Enter HourlyRate : ";
cin>>hourlyRate1;
if(hourlyRate1 < 0)
{
cout<<" Hourly pay rate cannot be negative.Enter again";
cin>>hourlyRate1;
}
emp.hourlyPaid.hourlyRate= hourlyRate1;
cout<<" Enter number of HoursWorked : ";
cin>>hoursWorked1;
if(hoursWorked1 < 0 || hoursWorked1 > 80)
{
cout<<" Hours Worked cannot be negative or greater than 80.Enter again";
cin>>hoursWorked1;
}
emp.hourlyPaid.hoursWorked = hoursWorked1;
cout<<" Total Pay = "<<(emp.hourlyPaid.hourlyRate * emp.hourlyPaid.hoursWorked);
}
return 0;
}
output:
Enter the type of Employee <1-salaried, 2-hourlyPaid>: 1
Enter salary : 4566.89
Enter bonus : 120.67
Total Pay = 4687.56
Enter the type of Employee <1-salaried, 2-hourlyPaid>: 2
Enter HourlyRate : 124.67
Enter number of HoursWorked : 55
Total Pay = 6856.85