In: Computer Science
Write a C++ 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 struct 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.
#include
using namespace std;
struct HourlyPaid
{
double hoursWorked;
double hourlyRate;
};
struct Salaried
{
double salary;
double bonus;
};
int main()
{
HourlyPaid hp;
Salaried s;
int choice;
cout << "1. Calculate pay for hourly" << endl;
cout << "2. Calculate pay for salaried" << endl;
cout << "Which type are you calculating (1 or 2): ";
cin >> choice;
if(choice == 1) //hourly
{
cout << "Enter the no. of hours (0-80): ";
cin >> hp.hoursWorked;
while(hp.hoursWorked < 0 || hp.hoursWorked > 80)
{
cout << "Hours worked should be in range 0-80. Please enter
again: ";
cin >> hp.hoursWorked;
}
cout << "Enter the hourly rate: ";
cin >> hp.hourlyRate;
while(hp.hourlyRate < 0)
{
cout << "Hourly rate can not be negative. Please enter again:
" ;
cin >> hp.hourlyRate;
}
double pay = hp.hoursWorked * hp.hourlyRate;
cout << "The pay is $" << pay << endl;
}
else if(choice == 2)
{
cout << "Enter the regular salary: ";
cin >> s.salary;
while(s.salary< 0 )
{
cout << "Salary can not be negative. Please enter again: "
;
cin >> s.salary;
}
cout << "Enter the bonus amount: ";
cin >> s.bonus;
while(s.bonus < 0)
{
cout << "Bonus can not be negative. Please enter again:
";
cin >> s.bonus;
}
double pay = s.salary + s.bonus;
cout << "The pay is $" << pay << endl;
}
else
cout << "Invalid choice! " << endl;
}
output
$./a.out
1. Calculate pay for hourly
2. Calculate pay for salaried
Which type are you calculating (1 or 2): 1
Enter the no. of hours (0-80): 40
Enter the hourly rate: 15
The pay is $600
$./a.out
1. Calculate pay for hourly
2. Calculate pay for salaried
Which type are you calculating (1 or 2): 2
Enter the regular salary: 3000
Enter the bonus amount: 400
The pay is $3400