Multipurpose Payroll I need to write a C++ program using Dev C++ that calculates
ID: 3632792 • Letter: M
Question
Multipurpose Payroll
I need to write a C++ program using Dev C++ 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:
Hours Worked
Hourly Rate
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 Hours Worked.
Explanation / Answer
Now check this code...
#include <cstdlib>
#include <iostream>
using namespace std;
struct HourlyPaid
{
float HoursWorked;
float HourlyRate;
};
struct Salaried
{
float Salary;
float Bonus;
};
union Employ
{
HourlyPaid hp;
Salaried sal;
};
int main(int argc, char *argv[])
{
char ch;
cout<<"Do you want to calculate payroll for Salaried(S)/Hourly Paid(H)?"<<endl;
cout<<"Select one option (S/H) : "<<endl;
cin>>ch;
Employ emp;
float PaidSalary = 0.0;
if(ch == 'S' || ch =='s')
{
while(1>0)
{
cout<<endl<<endl<<"Enter the salary for the employ : ";
cin>>emp.sal.Salary;
if(emp.sal.Salary < 0)
{
cout<<endl<<"Error : Salary can't be negative."<<endl;
}
else
{
PaidSalary = PaidSalary + emp.sal.Salary;
break;
}
}
while(1>0)
{
cout<<endl<<"Enter the bonus for the employ : ";
cin>>emp.sal.Bonus;
if(emp.sal.Bonus < 0)
{
cout<<endl<<"Error : Bonus can't be negative."<<endl;
}
else
{
PaidSalary = PaidSalary + emp.sal.Bonus;
break;
}
}
}
else
{
while(1>0)
{
cout<<endl<<"Enter the number of hours worked : ";
cin>>emp.hp.HoursWorked;
if(emp.hp.HoursWorked < 0)
{
cout<<endl<<"Error : Number hours worked can't be negative."<<endl;
}
else if(emp.hp.HoursWorked > 80)
{
cout<<endl<<"Error : Number hours should be less than or equal to 80."<<endl;
}
else
{
break;
}
}
while(1>0)
{
cout<<endl<<"Enter the hourly rate for the employ : ";
cin>>emp.hp.HourlyRate;
if(emp.hp.HourlyRate < 0)
{
cout<<endl<<"Error : Hourly Rate can't be negative."<<endl;
}
else
{
break;
}
}
PaidSalary = PaidSalary + emp.hp.HourlyRate * emp.hp.HoursWorked;
}
cout<<endl<<"Total Salary to be paid :"<<PaidSalary;
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.