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.]
Program
#include
int main (void)
{
/* Declare and initialize variables */
int pieces, code;
double total = 0;
float hours;
double pay, sales;
/* Prompt user to enter employee code, -1 to end
entries */
printf ("\nEnter employee's number code (-1 to end):
");
scanf ("%d", &code);
/* While -1 has not been entered */
while (code != -1)
{
/* Calculate employee's pay based
on pay code */
switch (code)
{
case 1: /*
Manager (pay code 1) */
/* Prompt user for manager's pay rate */
printf ("Enter the manager's pay rate:
$");
scanf ("%lf", &pay);
/* Print the weekly pay for the manager */
printf ("Weekly pay is: $ %.2lf\n\n",
pay);
/* Add manager's pay to total pay */
total += pay;
break;
case 2: /*
Hourly Worker (pay code 2)*/
/* Prompt user for hourly worker's pay rate
*/
printf ("Enter hourly worker's pay rate:
$");
scanf ("%lf", &pay);
/* Prompt user for number of hours worked
*/
printf ("Enter the number of hours worked:
");
scanf ("%f", &hours);
/* If hours worked is greater than 40,
calculate pay rate for first 40 hours,
then calculate pay rate
("time-and-a-half")
for hours worked over 40, and add these
values together. Otherwise, calculate
regular pay rate */
if (hours > 40)
pay = (pay * 40) + ((hours -
40) * (pay * 1.5));
else
pay = pay * hours;
/* Print the weekly pay for the employee
*/
printf ("Weekly pay is: $ %.2lf\n\n", pay);
/* Add weekly pay to total pay */
total += pay;
break;
case 3: /*
Commission employee (pay code 3) */
/* Prompt user for employee's gross weekly sales
*/
printf ("Enter commission employee's gross
weekly sales: ");
scanf ("%lf", &sales);
/* Calculate weekly pay for the employee
*/
pay = 250 + (.057 * sales);
/* Print the weekly pay */
printf ("Weekly pay is: $ %.2lf\n\n", pay);
/* Add pay to total */
total += pay;
break;
case 4: /*
Pieceworker (pay code 4) */
/* Prompt user for the number of pieces
completed */
printf ("\nEnter the number of pieces completed:
");
scanf ("%d", &pieces);
/* Prompt user for per piece pay rate */
printf ("Enter the employee's per piece pay
rate: $");
scanf ("%lf", &pay);
/* Calculate weekly pay for the employee */
pay = pieces * pay;
/* Print the weekly pay */
printf ("Weekly pay is: $ %.2lf\n\n", pay);
/* Add pay to total */
total += pay;
break;
default: /*
Invalid pay code */
/* Print error message */
printf ("You have entered an invalid
code.\n");
}
/* Prompt user to enter employee
code, -1 to end entries */
printf ("\nEnter employee's number
code (-1 to end): ");
scanf ("%d", &code);
}
/* Print the total pay for the week */
printf ("\nThe total payroll for the week is: $
%.2lf\n", total);
return 0;
}
Output
Enter employee's number code (-1 to end): 1
Enter the manager's pay rate: $3000
Weekly pay is: $ 3000.00
Enter employee's number code (-1 to end): 2
Enter hourly worker's pay rate: $25
Enter the number of hours worked: 45
Weekly pay is: $ 1187.50
Enter employee's number code (-1 to end): 3
Enter commission employee's gross weekly sales: 100
Weekly pay is: $ 255.70
Enter employee's number code (-1 to end): 4
Enter the number of pieces completed: 150
Enter the employee's per piece pay rate: $5
Weekly pay is: $ 750.00
Enter employee's number code (-1 to end): 5
You have entered an invalid code.
Enter employee's number code (-1 to end): -1
The total payroll for the week is: $ 5193.20