Hy, im having trouble with the following programming question. Program is done i
ID: 3865052 • Letter: H
Question
Hy, im having trouble with the following programming question.
Program is done in C
Multipurpose Payroll System Write a program that calculates pay for either an hourly paid worker or a salaried worker. Hourly paid workers are paid their hourly pay rate times the number of hours worked. Salaried workers are paid their regular salary plus any bonus they may have earned. The program should declare two structures for the following data:
Hourly Paid:
Name
Gender
HoursWorked
HourlyRate
Salaried:
Name
Age
Salary
Bonus
The program should also declare a union 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 the pay for an hourly paid worker or a salaried worker. Regardless of which the user selects, the appropriate member of the union will be used to store the data that will be used to calculate the pay.
Input Validation:
a) Do not accept negative numbers.
b) Do not accept values greater than 80 for HoursWorked.
c) Ensure to securely process numeric values
Explanation / Answer
#include <stdio.h>
union Employee //union with two structure variables
{
int category;
struct HourlyPaid
{
char name[30];
char gender;
int hoursWorked;
float hourlyRate;
}hp;
struct Salaried
{
char name[30];
int age;
float salary;
float bonus;
}s;
};
int main()
{
int category;
float salary;
union Employee emp;
printf("Are you calculating the pay for an hourly paid worker or a salaried worker? <1-hourly Employee , 2-Salaried Employee>");
scanf("%d",&category);
if(category == 1)
{
printf(" Enter the name of Employee : ");
scanf(" %[^ ]s",emp.hp.name); // sets delimiter of scanned string
printf(" Enter the gender : ");
scanf(" %c",&emp.hp.gender);
printf(" Enter the number of hours worked : ");
scanf("%d",&emp.hp.hoursWorked);
if(emp.hp.hoursWorked < 0 || emp.hp.hoursWorked>80)
{
printf(" The hour worked cannot be negative or greater than 80");
printf(" Enter the number of hours worked : ");
scanf("%d",&emp.hp.hoursWorked);
}
printf(" Enter the hourly pay rate : ");
scanf("%f",&emp.hp.hourlyRate);
if(emp.hp.hourlyRate < 0 )
{
printf(" The hourly pay rate cannot be negative");
printf(" Enter the hourly pay rate : ");
scanf("%f",&emp.hp.hourlyRate);
}
salary = emp.hp.hoursWorked * emp.hp.hourlyRate;
printf(" The salary of hourly paid worker %s is %.2f",emp.hp.name,salary);
}
else if(category == 2)
{
printf(" Enter the name of Employee : ");
scanf(" %[^ ]s",emp.s.name);
printf(" Enter the age : ");
scanf("%d",&emp.s.age);
printf(" Enter the salary : ");
scanf("%f",&emp.s.salary);
if(emp.s.salary <0)
{
printf(" The salary cannot be negative");
printf(" Enter the salary : ");
scanf("%f",&emp.s.salary);
}
printf(" Enter the bonus : ");
scanf("%f",&emp.s.bonus);
salary = emp.s.salary + emp.s.bonus;
printf(" The salary of salaried worker %s is %f",emp.s.name,salary);
}
return 0;
}
Output:
Are you calculating the pay for an hourly paid worker or a salaried worker? <1-hourly Employee , 2-Salaried Employee> 1
Enter the name of Employee : John Smith
Enter the gender : m
Enter the number of hours worked : 45
Enter the hourly pay rate : 12.2
The salary of hourly paid worker John Smith is 549.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.