In: Computer Science
A company pays its employees as managers (who receive a fixed weekly salary), hourly workers (who receive a fixed hourly wage for up to the first 40 hours they work and “time-and-a-half”—i.e., 1.5 times their hourly wage—for overtime hours worked), commission workers (who receive $250 plus 5.7% of their gross weekly sales), or pieceworkers (who receive a fixed amount of money for each of the items they produce—each pieceworker in this company works on only one type of item). Write a program to compute the weekly pay for each employee. You do not know the number of employees in advance.
Each type of employee has its own pay code: Managers have paycode 1, hourly workers have code 2, commission workers have code 3 and pieceworkers have code 4.
Use a switch to compute each employee’s pay based on that employee’s paycode. Within the switch, prompt the user (i.e., the payroll clerk) to enter the appropriate facts your program needs to calculate each employee’s pay based on that employee’s paycode. [Note: You can input values of type double using the conversion specifier %lf with scanf.]
The program should not be terminated or crashed (terminate abnormally) or enter into an infinite loop while processing invalid data types. It should continue to prompt the user to input the correct data type.
C/C+ language
C CODE:
#include <stdio.h>
#include<stdlib.h>
int main (void)
{
//variables to store the pieces, code, total pay,
weekly pay,sales,hours
int pieces, emp_code;
double total_pay = 0;
double pay, sales, hours;
//getting employee code from the user
printf ("\nEnter employee's number code (-1 to end):
");
// checking for valid employee code
while ( scanf("%d",&emp_code)!= 1)
{
printf("\nError.Invalid input.
Employee code must be 1,2,3,4 ");
printf("\n\nEnter employee's number
code (-1 to end): ");
fflush(stdin);
}
// while the code is not equal to -1
while (emp_code != -1)
{
// calculating the pay based on the
employee code
switch (emp_code)
{
case 1: printf
("\nEnter the manager's pay rate: ");
//checking for valid
input
while (scanf("%lf", &pay)
!= 1)
{
printf("\nError.Invalid input. Pay rate must be a number. ");
printf("\n\nEnter the manager's pay rate: ");
fflush(stdin);
}
// printing the weekly
pay
printf ("Weekly pay:
%.2f\n\n", pay);
//adding the pay to the
total
total_pay += pay;
break;
case 2:
printf ("\nEnter hourly worker's pay rate: ");
//checking for valid
input
while (scanf("%lf", &pay)
!= 1)
{
printf("\nError.Invalid input. Pay rate must be a number. ");
printf("\n\nEnter hourly worker's pay rate: ");
fflush(stdin);
}
// number of hours
worked
printf ("\nEnter the number
of hours worked: ");
// checking for valid
input
while (scanf("%lf",
&hours) != 1)
{
printf("\nError.Invalid input. Hours must be a number. ");
printf("\n\nEnter the number of hours worked: ");
fflush(stdin);
}
// calculating the pay
if (hours > 40)
pay = (pay
* 40) + ((hours - 40) * (pay * 1.5));
else
pay = pay
* hours;
//printing the weekly
pay
printf ("Weekly pay:
%.2f\n\n", pay);
//adding the pay to the
total
total_pay += pay;
break;
case 3:
//getting commission employee sales details
printf ("\nEnter commission
employee's gross weekly sales: ");
// checking for valid
input
while (scanf("%lf",
&sales) != 1)
{
printf("\nError.Invalid input. Weekly sales must be a number.
");
printf("\n\nEnter commission employee's gross weekly sales:
");
fflush(stdin);
}
//calculating the weekly
pay
pay = 250 + (.057 *
sales);
//displaying the weekly
pay
printf ("Weekly pay:
%.2f\n\n", pay);
//adding the pay to the
total
total_pay += pay;
break;
case 4:
//getting number of pieces
printf ("\nEnter the number
of pieces completed: ");
// checking for valid int
input
while (scanf("%d",
&pieces) != 1)
{
printf("\nError.Invalid input. Number of pieces must be a number.
");
printf("\n\nEnter the number of pieces completed: ");
fflush(stdin);
}
//getting pay rate for every
piece
printf ("\nEnter the
employee's per piece pay rate: ");
//checking for valid
input
while (scanf("%lf", &pay)
!= 1)
{
printf("\nError.Invalid input. Pay rate must be a number. ");
printf("\n\nEnter the employee's per piece pay rate: ");
fflush(stdin);
}
// calculating the weekly pay
pay = pieces * pay;
// printing the pay
printf ("Weekly pay:
%.2f\n\n", pay);
// adding the pay to the total
total_pay += pay;
break;
default:
printf ("\nError: You have entered an invalid code.\n");
}
// getting the employee
code
printf ("\nEnter employee's number
code (-1 to end): ");
scanf ("%d", &emp_code);
}
// printing the total pay
printf ("\nThe total payroll for the week: %.2f\n",
total_pay);
return 0;
}
SCREENSHOT FOR OUTPUT: