In: Computer Science
Assignment: (Save this file as A7-1.cpp) Write a program to calculate the gross pay for an assembly line employee that works for a company that pays all assembly line workers $7.50 hour. Any employee that works over 40 hours per week is compensated by being paid time-and-one-half for each hour over 40. a. Use main( ) as the driver function. Allow the user to compute as many employees as desired. b. Write the function getName( ) that prompts for the name of the employee and returns it to main( ). c. Write the function getTime( ) that prompts the user for the time worked for that week and returns this value back to main( ). Do not allow the user to enter a negative value for the hours. Display an error message and keep prompting until a valid value is entered. d. Write the function computePay( ) that calculates the gross pay of the employee using the time entered by the user and returns the calculated gross pay back to main( ). e. Write the function displayPay( ) that takes as input the employee’s name, the gross pay and time worked from main( ) and displays these values.
ANSWER:
CODE
// import libraries
#include <bits/stdc++.h>
// using namespace
using namespace std;
// b. function getName
string getName(){
// prompting user to enter employee name
cout<<"Please enter name of the employee: ";
// storing name in variable name
string name;
// taking input
cin>>name;
// returning name
return name;
}
// c. function getTime
float getTime(){
// loop till input time is correct
while(1){
// prompting user to enter employee name
cout<<"Please enter working hours of the employee: ";
// storing result in float hours
float hours;
// taking input
cin>>hours;
// checking if hours is correct
if(hours>=0){
// return hours
return hours;
}
// otherwise prompt user to enter again
cout<<"Negative working hours, Retry..."<<endl;
}
}
// d. function computePay, to compute gross according to hours
float computePay(float hours){
// defining variable grossPay
float grossPay;
// payment calculation
if(hours<=40){
grossPay = hours * 7.5;
} // otherwise > 40
else{
// 7.5 till 40 hours and for remaining hours 7.5*1.5
grossPay = (40 * 7.5) + (grossPay-40)*7.5*1.5;
}
// return grossPay
return grossPay;
}
// e. function displayPay, to display all details of employee
void displayPay(string name, float hours, float grossPay){
// displaying result
cout<<name<<" gross pay for working "<<hours<<" hours is: $"<<grossPay;
cout<<endl;
}
// a. function main, Driver Function
int main(){
// while loop, till user want to enter employee details
// defining variable for run for more employee
int more = 1;
while(more){
// b. getName function call to ask employee name
string name = getName();
// c. getTime function call to ask +ve working hours
float hours = getTime();
// d. computePay function call to fetch grossPay
float grossPay = computePay( hours);
// e. diplay function to display result
displayPay(name,hours,grossPay);
// prompting user to enter more employee or not
cout<<"Do you want to continue <1,0>: ";
cin>>more;
}
}
OUTPUT IMAGE