Hello, I need help in c programming.
the question was: ( calculating payroll)
Management:
$40/hr
30% tax rate
Developers
$30/hr
25% tax rate
Human Resources
$22/hr
25% tax rate
The program must employ a nested loop and use functions to determine the pay and tax rate for
an employee.
Sample Output:
How Many Employees? 10
Employee #1:
Department: M
Enter hours for each day:
Day 1: 6
Day 2: 8
Day 3: 7.5
Day 4: 8
Day 5: 4.5
Employee #2:
Department: H
. . .
Initech Payroll: $38209.89
Management Payroll: $10321.33
Human Resources Payroll: $5432.12
Developers Payroll: $22456.44
I wrote the program, it works, but I have some questions:
include<stdio.h>
int main(void)
{
double total_pay; //company payroll
int count_emp; //current employee
int num_emp; //number of employees
double hours; //hours worked
double rate; //hourly rate
char dep; //which department
double pay; //pay for this period
double tax;
printf("please, Enter number of employees: ");
scanf("%d",&num_emp);
int i=1,j=1;
for(i=1;i<=num_emp;i++)
{ printf("Employee# %d \n\n",i);
printf("Which department(m,d,h): ");
scanf(" %c",&dep);
printf("Enter hours per each day: \n\n");
for(j=1;j<6;j++)
{
printf("Day %d : \n",j);
scanf("%lf",&hours);
if(dep=='m'){
pay=hours40;
tax=pay0.3;
pay=pay-tax;
}
else if(dep=='d'){
pay=hours30;
tax=pay0.25;
pay=pay-tax;
}
else{
pay=hours22;
tax=pay0.25;
pay=pay-tax;
}
total_pay=total_pay+pay;
}
}
printf("Initech Payroll: $%8.2f\n",total_pay);
return 0;
}
First: when it asks for what department, I can enter any letter any number, ( which will go to the else if statement), how can I make the program only accept those three letters( can I use do-while)
Second: I couldn't make the last part of :
Initech Payroll: $38209.89
Management Payroll: $10321.33
Human Resources Payroll: $5432.12
Developers Payroll: $22456.44
Third: how can I adjust this program, so it works using user defined functions?
Sorry, I'm still kinda beginner in c programming.
Thanks for your time.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Oh thanks Clifford,
those ( >) just appeared after copying it from my e-mail, I don't know.
I really appreciate your help.
I'll try to start rewriting the program using functions, thanks again for your time.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
printf("Which department(m,d,h): ");
do
{
scanf(" %c",&dep);
} while( dep != 'm' && dep != 'd' && dep != 'h' ) ;
> Second: I couldn't make the last part of
Instead of total_pay, you might have three variables mgmnt_pay, hr_pay, dev_pay, then in each clause of the if/else if/else, you add to teh appropriate variable. The total is the sum of all three:
Note that instead of if/else if/ else, a switch/case would be more appropriate here (testing a single variable against an integer constant (a character is an integer constant BTW). I would not leave HR pay to the default or else clause, but rather have it explicitly tested, leaving the default/else clause for 'shit happens' defensive coding. It also makes it easier to expand to new departments if necessary (although there are better designs for handling such extensibility).
> ... so it works using user defined functions?
What do you mean exactly?
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks cpns,
Thanks for the clarification.
I mean user defined function, that what my teacher asked us to do, like one functions for the department, one function for the adding the hours per week, and so on, then use them in the program, by calling them, I have a problem in doing it this way,
I will try to adjust the program and post back, so you can check it, if you don't mind.
thanks again
Have a nice day Cpns
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ah, you simply mean how do you functionally decompose your program. The term 'user defined' here is ambiguous; I thought perhaps you meant the 'user' of the program, not the programmer (i.e. the 'user' of the compiler). In general:
void aFunction( void ) ; // a function prototype
int main()
{
aFunction() ; // a function call
}
// A function definition
void aFunction(void)
{
// some code
}
Hello Clifford ,
I edited the code, it works, but there's something not right, when I don't enter anything for specific department, it says - number, I don't know what's wrong with it.
If you have enough time, can you please take a look over it.
Thanks
> #include<stdio.h>
> int main(void)
> {
> double total_pay; //company payroll
> int num_emp; //number of employees
> double hours; //hours worked
> char dep; //which department
> double pay; //pay for this period
> double mgmnt_pay,hr_pay,dev_pay; // departments
> printf("please, Enter number of employees: \n");
> scanf("%d",&num_emp);
> int i=1,j=1;
> for(i=1;i<=num_emp;i++)
> { printf("Employee# %d \n\n",i);
> printf(" IniTech Departments \n");
> printf("m : Management.\n");
> printf("d : Developers.\n");
> printf("h : Human Resources.\n");
> do
> {
> printf("Please, Enter m or d or h for your
> department\n");
> scanf(" %c",&dep);
> } while( dep != 'm' && dep != 'd' && dep != 'h' ) ;
> printf("Enter hours per each day: \n\n");
> for(j=1;j<6;j++)
> {
> printf("Day %d : \n",j);
> scanf("%lf",&hours);
> if(dep=='m'){
> mgmnt_pay=hours40;
> mgmnt_pay-=mgmnt_pay0.3;
> }
> else if(dep=='d'){
> dev_pay=hours30;
> dev_pay-=dev_pay0.25;
> }
> else{
> hr_pay=hours22;
> hr_pay-=hr_pay0.25;
> }
> }
> }
> printf("Initech Payroll: $%8.2f\n\n",mgmnt_pay+hr_pay+dev_pay);
> printf("Management Payroll: $%8.2f\n",mgmnt_pay);
> printf("Human Resources Payroll: $%8.2f\n",hr_pay);
> printf("Developers Payroll: $%8.2f\n",dev_pay);
> return 0;
> }
> =====================================================================
>
> please, Enter number of employees:
> 2
> Employee# 1
>
> IniTech Departments
> m : Management.
> d : Developers.
> h : Human Resources.
> Please, Enter m or d or h for your department
> h
> Enter hours per each day:
>
> Day 1 :
> 3
> Day 2 :
> 3
> Day 3 :
> 1
> Day 4 :
> 1
> Day 5 :
> 1
> Employee# 2
>
> IniTech Departments
> m : Management.
> d : Developers.
> h : Human Resources.
> Please, Enter m or d or h for your department
> m
> Enter hours per each day:
>
> Day 1 :
> 2
> Day 2 :
> 2
> Day 3 :
> 2
> Day 4 :
> 3
> Day 5 :
> 1
> Initech Payroll: $ 44.26
>
> Management Payroll: $ 28.00
> Human Resources Payroll: $ 16.50
> Developers Payroll: $ -0.24
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You now also have some unused variables. (pay, and total_pay) for example. You should clean that up.
Note that the unused variable and in some cases uninitialised variable problems can be caught by the compiler. Set the -Wall -Wformat -Werror compiler options to get teh compiler to help you find more bugs. Another way of finding extra errors is to switch optimisation on, the optimiser does a far deeper analysis of teh code and can sometimes pick out more complex errors.
However the best tool for debugging is not surprisingly a debugger. It shoudl be teh second thing you learn to use in programming after the compiler (pretty much after "hello, world"). In this case you would have seen from the very start that these variables start with junk values. Unfortunately Dev-C++'s debugger is its weakest feature and is barely usable. Use Insight from the www.mingw.org project's 'contributed' files, or better yet use VC++ instead of Dev-C++ - its debugger is second to none.
Don't add > markers to your code. It makes it far more difficult to copy, paste, and compile it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello, I need help in c programming.
the question was: ( calculating payroll)
Management:
$40/hr
30% tax rate
Developers
$30/hr
25% tax rate
Human Resources
$22/hr
25% tax rate
The program must employ a nested loop and use functions to determine the pay and tax rate for
an employee.
Sample Output:
How Many Employees? 10
Employee #1:
Department: M
Enter hours for each day:
Day 1: 6
Day 2: 8
Day 3: 7.5
Day 4: 8
Day 5: 4.5
Employee #2:
Department: H
. . .
Initech Payroll: $38209.89
Management Payroll: $10321.33
Human Resources Payroll: $5432.12
Developers Payroll: $22456.44
I wrote the program, it works, but I have some questions:
include<stdio.h>
int main(void)
{
double total_pay; //company payroll
int count_emp; //current employee
int num_emp; //number of employees
double hours; //hours worked
double rate; //hourly rate
char dep; //which department
double pay; //pay for this period
double tax;
printf("please, Enter number of employees: ");
scanf("%d",&num_emp);
int i=1,j=1;
for(i=1;i<=num_emp;i++)
{ printf("Employee# %d \n\n",i);
printf("Which department(m,d,h): ");
scanf(" %c",&dep);
printf("Enter hours per each day: \n\n");
for(j=1;j<6;j++)
{
printf("Day %d : \n",j);
scanf("%lf",&hours);
if(dep=='m'){
pay=hours40;
tax=pay0.3;
pay=pay-tax;
}
else if(dep=='d'){
pay=hours30;
tax=pay0.25;
pay=pay-tax;
}
else{
pay=hours22;
tax=pay0.25;
pay=pay-tax;
}
total_pay=total_pay+pay;
}
}
printf("Initech Payroll: $%8.2f\n",total_pay);
return 0;
}
First: when it asks for what department, I can enter any letter any number, ( which will go to the else if statement), how can I make the program only accept those three letters( can I use do-while)
Second: I couldn't make the last part of :
Initech Payroll: $38209.89
Management Payroll: $10321.33
Human Resources Payroll: $5432.12
Developers Payroll: $22456.44
Third: how can I adjust this program, so it works using user defined functions?
Sorry, I'm still kinda beginner in c programming.
Thanks for your time.
Oh thanks Clifford,
those ( >) just appeared after copying it from my e-mail, I don't know.
I really appreciate your help.
I'll try to start rewriting the program using functions, thanks again for your time.
printf("Which department(m,d,h): ");
do
{
scanf(" %c",&dep);
} while( dep != 'm' && dep != 'd' && dep != 'h' ) ;
> Second: I couldn't make the last part of
Instead of total_pay, you might have three variables mgmnt_pay, hr_pay, dev_pay, then in each clause of the if/else if/else, you add to teh appropriate variable. The total is the sum of all three:
printf("Initech Payroll: $%8.2f\n", mgmnt_pay + hr_pay + dev_pay );
and the rest is obvious I hope.
Note that instead of if/else if/ else, a switch/case would be more appropriate here (testing a single variable against an integer constant (a character is an integer constant BTW). I would not leave HR pay to the default or else clause, but rather have it explicitly tested, leaving the default/else clause for 'shit happens' defensive coding. It also makes it easier to expand to new departments if necessary (although there are better designs for handling such extensibility).
> ... so it works using user defined functions?
What do you mean exactly?
Clifford
Thanks cpns,
Thanks for the clarification.
I mean user defined function, that what my teacher asked us to do, like one functions for the department, one function for the adding the hours per week, and so on, then use them in the program, by calling them, I have a problem in doing it this way,
I will try to adjust the program and post back, so you can check it, if you don't mind.
thanks again
Have a nice day Cpns
Ah, you simply mean how do you functionally decompose your program. The term 'user defined' here is ambiguous; I thought perhaps you meant the 'user' of the program, not the programmer (i.e. the 'user' of the compiler). In general:
void aFunction( void ) ; // a function prototype
int main()
{
aFunction() ; // a function call
}
// A function definition
void aFunction(void)
{
// some code
}
So you might define a function say:
devPay( double hours )
{
double pay = hours * 30 ;
pay -= pay * 0.25 ;
return pay ;
}
and then in main have:
else if(dep=='d'){
pay = devPay( hours ) ;
}
Clifford
Hello Clifford ,
I edited the code, it works, but there's something not right, when I don't enter anything for specific department, it says - number, I don't know what's wrong with it.
If you have enough time, can you please take a look over it.
Thanks
> #include<stdio.h>
> int main(void)
> {
> double total_pay; //company payroll
> int num_emp; //number of employees
> double hours; //hours worked
> char dep; //which department
> double pay; //pay for this period
> double mgmnt_pay,hr_pay,dev_pay; // departments
> printf("please, Enter number of employees: \n");
> scanf("%d",&num_emp);
> int i=1,j=1;
> for(i=1;i<=num_emp;i++)
> { printf("Employee# %d \n\n",i);
> printf(" IniTech Departments \n");
> printf("m : Management.\n");
> printf("d : Developers.\n");
> printf("h : Human Resources.\n");
> do
> {
> printf("Please, Enter m or d or h for your
> department\n");
> scanf(" %c",&dep);
> } while( dep != 'm' && dep != 'd' && dep != 'h' ) ;
> printf("Enter hours per each day: \n\n");
> for(j=1;j<6;j++)
> {
> printf("Day %d : \n",j);
> scanf("%lf",&hours);
> if(dep=='m'){
> mgmnt_pay=hours40;
> mgmnt_pay-=mgmnt_pay0.3;
> }
> else if(dep=='d'){
> dev_pay=hours30;
> dev_pay-=dev_pay0.25;
> }
> else{
> hr_pay=hours22;
> hr_pay-=hr_pay0.25;
> }
> }
> }
> printf("Initech Payroll: $%8.2f\n\n",mgmnt_pay+hr_pay+dev_pay);
> printf("Management Payroll: $%8.2f\n",mgmnt_pay);
> printf("Human Resources Payroll: $%8.2f\n",hr_pay);
> printf("Developers Payroll: $%8.2f\n",dev_pay);
> return 0;
> }
> =====================================================================
>
> please, Enter number of employees:
> 2
> Employee# 1
>
> IniTech Departments
> m : Management.
> d : Developers.
> h : Human Resources.
> Please, Enter m or d or h for your department
> h
> Enter hours per each day:
>
> Day 1 :
> 3
> Day 2 :
> 3
> Day 3 :
> 1
> Day 4 :
> 1
> Day 5 :
> 1
> Employee# 2
>
> IniTech Departments
> m : Management.
> d : Developers.
> h : Human Resources.
> Please, Enter m or d or h for your department
> m
> Enter hours per each day:
>
> Day 1 :
> 2
> Day 2 :
> 2
> Day 3 :
> 2
> Day 4 :
> 3
> Day 5 :
> 1
> Initech Payroll: $ 44.26
>
> Management Payroll: $ 28.00
> Human Resources Payroll: $ 16.50
> Developers Payroll: $ -0.24
Non-static (auto) variables are not initialised to zero, so have indeterminate initial value in this declaration.
double mgmnt_pay,hr_pay,dev_pay; // departments
Crowding all your declarations in a single statement in most cases is not a good idea for reasons of clarity and maintainability.
// departments pay
double mgmnt_pay = 0 ;
double hr_pay = 0 ;
double dev_pay = 0 ;
You now also have some unused variables. (pay, and total_pay) for example. You should clean that up.
Note that the unused variable and in some cases uninitialised variable problems can be caught by the compiler. Set the -Wall -Wformat -Werror compiler options to get teh compiler to help you find more bugs. Another way of finding extra errors is to switch optimisation on, the optimiser does a far deeper analysis of teh code and can sometimes pick out more complex errors.
However the best tool for debugging is not surprisingly a debugger. It shoudl be teh second thing you learn to use in programming after the compiler (pretty much after "hello, world"). In this case you would have seen from the very start that these variables start with junk values. Unfortunately Dev-C++'s debugger is its weakest feature and is barely usable. Use Insight from the www.mingw.org project's 'contributed' files, or better yet use VC++ instead of Dev-C++ - its debugger is second to none.
Don't add > markers to your code. It makes it far more difficult to copy, paste, and compile it.