I am working on an employee pay program in C and I am trying to find a better way of organizing the code for calculating the pay for the Hourly Worker. I want to use a switch statement, instead of a boatload of if statements. I was just wondering if that was possible? Would the cases need a value or can it stay blank? For example, could I use just "case:" instead of "case value1:"? I know that if I do use a switch statement that I will probably have to use if's to validate that what the user put in is valid. Or should I just stick with a bunch of if's?
In case you were wondering, here are the directions that I need to follow:
- A company pays its employees as: a) managers, who receive a fixed weekly salary; b) 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; c) commission workers, who receive $250 plus 5.7% of their weekly sales; or d) pieceworkers, who receive a fixed amount per item for each item produced.
Write a program to compute weekly pay for each employee. You don’t know the number of employees in advance. Each employee type has their own pay code:
1 for Managers
2 for Hourly Workers
3 for Commission Workers
4 for Pieceworkers.
Write a separate function to handle each paycode type. Use a switch to
compute each employee's pay based on that employee's paycode. Within
the switch, prompt the user to enter the appropriate facts the program
needs to calculate each employee's pay based on that employee's paycode.
Add reasonableness checks as appropriate (e.g., salary between 0 - 100000,
hours worked between 0 – 84, hourly rate between 0 – 250, weekly sales
between 0 – 1000000, number of pieces between 1 – 1000, wage per piece
between 0.01 – 1000. Use constants).
Here are the program constants: #include
// constants
#define MANAGERS 1
#define HOURLY_WORKERS 2
#define COMMISSION_WORKERS 3
#define PIECEWORKERS 4
#define STOP_PROGRAM -1
#define COMMISSION_PERCENT 0.057
#define COMMISSION_PAY 250
#define OVERTIME_HOURS 1.5
#define MAX_HOURS_WORKED 40
Here is the code I have so far for the Hourly Worker function:
void calcHourlyWorkerPay()
{
float hourlyPayRate;
int totalHoursWorked;
float totalPay;
printf("You entered 2 for Hourly Worker.\n\n");
printf("Enter hourly pay rate.\n"
"(Entry must be between 0 and 250): ");
scanf ("%f", &hourlyPayRate);
printf("Enter total hours worked.\n"
"(Entry must be between 0 and 84): ");
scanf ("%d", &totalHoursWorked);
if ((totalHoursWorked <= MAX_HOURS_WORKED) && (totalHoursWorked >= 84))
{
}
}
The reason I am asking is because I want to add validation to this where if the user enters a number that is not between 0 and 84, it will say something like "Entry invalid, please enter a number between 0 and 84." I also want to add validation where if the user enters an hourly rate that is not between 0 and 250, it will say something like "Entry invalid, please enter a number between 0 and 250." I hope I didn't confuse anyone. I probably made a few mistakes, and I should point out that I am VERY new to C programming.
EDITED
I edited the if statement as follows:
if (((totalHoursWorked >= 0) && (totalHoursWorked <= 84)) && ((hourlyPayRate >= 0) && (hourlyPayRate <= 250)))
{
totalPay = totalHoursWorked * hourlyPayRate;
}
else if ((totalHoursWorked <= 0) || (totalHoursWorked >= 84))
{
printf("\n\nINVALID!!! Total hours worked MUST be between 0 and 84.\n\n");
printf("Enter total hours worked.\n"
"(Entry must be between 0 and 84): ");
scanf ("%d", &totalHoursWorked);
}
else if ((hourlyPayRate <= 0) || (hourlyPayRate >= 250))
{
printf("\n\nINVALID!!! Hourly pay rate MUST be between 0 and 250.\n\n");
printf("Enter hourly pay rate.\n"
"(Entry must be between 0 and 250): ");
scanf ("%f", &hourlyPayRate);
}
else
{
totalPay = (MAX_HOURS_WORKED * hourlyPayRate) + ((hoursWorked - MAX_HOURS_WORKED) * hourlyPayRate * OVERTIME_HOURS);
}
Is that correct and will it work?