In: Computer Science
Write a program that calculates pay for either hourly paid workers or salaried workers. Hourly paid workers are paid their hourly pay rate times the number of hours worked. Salaried workers are payed 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 structure 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 pay for an hourly paid worker or a salaried worker. Regardless of what 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 hours worked.
Optional: you may use C++ objects in place of structs to represent salaried and hourly employees
USING C++
This below script is written as per given in the probelm statement.ie created two structures for salary and hourly workeers seperately and calculated the salary and displayed the result and validated the number hours should be positive and not greter than 80..
Code:
#include<iostream>
using namespace std;
// Created HourlyWorker Structure with Hours worked and
payrate
struct HourlyWorker
{
private:
int HoursWorked;
int HourlyRate;
public:
// Initialise the data members with default
values
HourlyWorker()
{
HoursWorked = 0;
HourlyRate = 0;
}
// Initialise the data members from the object
HourlyWorker(int a, int b)
{
HoursWorked = a;
HourlyRate = b;
}
// Calculating Salary
void calculate()
{
cout<<"Hourly Worker Payment is: "<<HourlyRate *
HoursWorked<<endl;
}
};
// Created SalaryWorker Structure with Salary worked and
Bonus
struct SalaryWorker
{
private:
int Salary;
int Bonus;
public:
// Initialise the data members with default values
SalaryWorker()
{
Salary = 0;
Bonus = 0;
}
// Initialise the data members from the object
SalaryWorker(int a, int b)
{
Salary = a;
Bonus = b;
}
// Calculating Salary
void calculate()
{
cout<<"Salaried Employee Salary with Bonus is:
"<<Salary + Bonus <<endl;
}
};
int main()
{
cout<<"Enter Number of Hours in Positive only and not greater
than 80 Hours:"<<endl;
int hour;
cin>>hour;
// Checking the number of hours are postive and not
greater than 80
if(80 >hour && 0 <hour)
{
HourlyWorker h(hour,10);
h.calculate();
SalaryWorker s(50,20);
s.calculate();
}
else
{
cout<<"Re-Enter Hours"<<endl;
cin>>hour;
HourlyWorker h(hour,10);
h.calculate();
SalaryWorker s(50,20);
s.calculate();
}
return 0;
}
Output: