In: Computer Science
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.
#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<<"\nEnter the type of Employee <1-salaried,
2-hourlyPaid>: ";
cin>>employeeType;
if(employeeType == 1)
{
cout<<"\nEnter salary : ";
cin>>salary1;
if(salary1 < 0)
{
cout<<"\nSalary cannot be negative.Enter again";
cin>>salary1;
}
emp.salaried.salary = salary1;
cout<<"\nEnter bonus : ";
cin>>bonus1;
if(bonus1 < 0)
{
cout<<"\nbonus cannot be negative.Enter again";
cin>>bonus1;
}
emp.salaried.bonus = bonus1;
cout<<"\nTotal Pay = "<<(emp.salaried.salary +
emp.salaried.bonus);
}
else if(employeeType == 2)
{
cout<<"\nEnter HourlyRate : ";
cin>>hourlyRate1;
if(hourlyRate1 < 0)
{
cout<<"\nHourly pay rate cannot be negative.Enter
again";
cin>>hourlyRate1;
}
emp.hourlyPaid.hourlyRate= hourlyRate1;
cout<<"\nEnter number of HoursWorked : ";
cin>>hoursWorked1;
if(hoursWorked1 < 0 || hoursWorked1 > 80)
{
cout<<"\nHours Worked cannot be negative or greater than
80.Enter again";
cin>>hoursWorked1;
}
emp.hourlyPaid.hoursWorked = hoursWorked1;
cout<<"\nTotal 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