A bookstore owner wants you to write a program to calculate the store's weekly payroll. The store's employees are paid hourly. Overtime is to be paid at the rate of 1.5 times the normal rate for hours an
employee worked over 40.
The program should prompt the user to enter the number of hours the employee worked and the payrate for the employee. The program should display the regular pay, overtime pay, and gross pay for that employee. Then the program should prompt the user to enter the data for another employee. When theuser responds that there are no more employees to process, the program should display the number of employees it processed and the total payroll, that is, the sum of the gross salaries of the employees
The program should validate pay rate as it is entered. The only valid hourly pay rates are the following:
5.50, 6.00, 6.50, 6.75, 7.00, 7.25, 7.50, 7.75, and 8.00.
Store the rates in an array. When a pay rate is entered, search the array for a match. If there is a match, continue the rest of the calculations. If there is
no match, display an error message, a list of the valid pay rates, and prompt for another pay rate.
Here is my code. somehow it works. But I not sure whether my calculations are right or not and I want to find a way more efficient way to write this program.
Advice are much appreciated. :D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include<iostream>
#include<iomanip>
#include<cmath>
double calc_pay(int hour,double payrate);
using namespace std;
int main()
{
int k=0,hour;
double A_payrate[]={5.50, 6.00, 6.50, 6.75, 7.00, 7.25, 7.50, 7.75, 8.00};
char choice='y';
double grosspay=0,payrate,total_payroll=0;
while(1)
{
cout<<"For employee #"<<k+1<<": "<<endl;
cout<<"Enter the number of working hours: ";
cin>>hour;
do
{
cout<<"Enter the Hourly pay rate: Choose from below: \n"<<endl;
cout<<"( 5.50, 6.00, 6.50, 6.75, 7.00, 7.25, 7.50, 7.75, 8.00 ): ";
cin>>payrate;
if(payrate==5.50 || payrate==6.00 || payrate==6.50 || payrate==6.75 || payrate==7.00 || payrate==7.25 ||
payrate==7.50 || payrate==7.75 || payrate==8.00)
{
cout<<"You have selected "<<setprecision(2)<<fixed<<payrate<<" as payrate "<<endl;
}
if(!(payrate==5.50 || payrate==6.00 || payrate==6.50 || payrate==6.75 || payrate==7.00 || payrate==7.25 ||
payrate==7.50 || payrate==7.75 || payrate==8.00))
{
cout<<"Invalid Payrate. Please reenter again : ";
cin>>payrate;
cout<<"You have selected "<<setprecision(2)<<fixed<<payrate<<" as payrate "<<endl;
}
} while(payrate==5.50 && payrate==6.00 && payrate==6.50 && payrate==6.75 && payrate==7.00 && payrate==7.25 &&
payrate==7.50 && payrate==7.75 && payrate==8.00);
grosspay = calc_pay(hour,payrate);
k++;
total_payroll += grosspay;
cout<<"\nContinue? (y/n) : ";
cin>>choice; cout<<endl;
if(choice != 'y')
{
cout<<"You have selected n to continue."<<endl;
break;
}
}
cout<<fixed<<setprecision(2);
cout<<"The total No of Employee is "<<k<<endl;
cout<<"The total payroll "<<total_payroll<<endl;
system("Pause");
return 0;
}
double calc_pay(int hour,double payrate)
{
if(hour>40)
{
return 40 * payrate + (hour-40) * 1.5 * payrate;
}
else
{
return hour*payrate;
}
}
|