View Full Version : [C] Computer Science assignment- Payroll program.
alvinleephd
02-09-2006, 11:16 PM
I have to create a payroll program for an introductory computer science course I'm taking at my college.
Here's the format:
_______________________________________________________________
Payroll Program for X Y Z Corp.
Enter hours worked --> 10.0
Enter hourly rate --> 20.0
This employee worked 10.0 hours with a salary of $20 per hour. Here is the salary statement for this employee:
Gross pay: 200
Social security tax: -12
Federal tax: -20
State tax: -10
Medical/dental -10:
---------------------------------
Net pay: 148.00
____________________________________________
this is what i have so far:
#include <stdio.h>
int main ()
{
int x;
int y;
int Gross_Pay;
int Social_security_tax;
int Federal_tax;
int State_tax;
int Medical_dental;
int Net_Pay;
printf("Payroll Program for X Y Z Corp.\n");
{
printf("Enter hours worked: ");
scanf("%d", &x);
printf("Enter hourly rate: ");
scanf("%d", &y);
}
printf("This employee worked %d hours with a salary of $%d per hour. Here is the salary statement for this employee:\n",x,y);
Gross_Pay=x*y;
printf("Gross Pay: %d * %d = %d\n", x, y, x * y);
Social_security_tax=x*y*.06;
printf("Social security tax: %d * %d * .06= -%d\n",x,y,Social_security_tax);
Federal_tax=x*y*.1;
printf("Federal tax: %d * %d * .1= -%d\n",x,y,Federal_tax);
State_tax=x*y*.05;
printf("State tax: %d * %d * .05= -%d\n",x,y,State_tax);
Medical_dental=x*y*.05;
printf("Medical/dental: %d * %d * .05= -%d\n",x,y,Medical_dental);
Net_Pay=x*y-(x*y*.06)-(x*y*.1)-(x*y*.05)-(x*y*.05);
printf("Net Pay: %d * %d - (%d * %d *.06) - (%d * %d * .1) - (%d * %d * .05) - (%d * %d * .05)= %d\n",x,y,Net_Pay);
return 0;
}
Everything seems to be working fine, but there are a few problems. I'd like the results to show without the equations.
For example, i want it to be like this:
Gross pay: result, not Gross pay: x*y=result
also, i can't seem to create a working code for the calculation of the net pay (gross pay - taxes and medical dental)
any help would be appreciated. thanks in advance!
i understand that this probably pretty basic stuff, but it's all new to me so you have to bear with me :o
_Aerospace_Eng_
02-09-2006, 11:53 PM
Did you write the program yourself? I'll show you how to display Gross Pay: num rather than the equation but the others you will need to figure out.
printf("Gross Pay: %d\n", Gross_Pay);
Notice how inside the quotes you have Gross Pay: and then you have %d this means that an integer will need to be passed to the printf statement, this is why I have included Gross_Pay inside the parentheses. Note you have Gross_Pay set as x*y so %d should be x*y.
Good luck.
BTW in the future it is good to say what language you are coding in. I know its C but thats because I've taken C programming
alvinleephd
02-10-2006, 12:11 AM
thanks it works like a charm!
everything seems to be in place but i need to get the formatting correct
here's what i have:
#include <stdio.h>
int main ()
{
int x;
int y;
int Gross_Pay;
int Social_security_tax;
int Federal_tax;
int State_tax;
int Medical_dental;
int Net_Pay;
printf("Payroll Program for X Y Z Corp.\n");
{
printf("Enter hours worked: ");
scanf("%d", &x);
printf("Enter hourly rate: ");
scanf("%d", &y);
}
printf("This employee worked %d hours with a salary of $%d per hour. Here is the salary statement for this employee:\n",x,y);
Gross_Pay=x*y;
printf("Gross Pay: %d\n", Gross_Pay);
Social_security_tax=x*y*.06;
printf("Social security tax: -%d\n", Social_security_tax);
Federal_tax=x*y*.1;
printf("Federal tax: -%d\n", Federal_tax);
State_tax=x*y*.05;
printf("State tax: -%d\n", State_tax);
Medical_dental=x*y*.05;
printf("Medical/dental: -%d\n", Medical_dental);
Net_Pay=x*y-(x*y*.06)-(x*y*.1)-(x*y*.05)-(x*y*.05);
printf("Net Pay: %d\n", Net_Pay);
return 0;
}
how can i add a line under the statement and before net pay?
like so:
grosspay
ss tax
federal tax
state tax
medical/dental
----------------
net pay
_Aerospace_Eng_
02-10-2006, 12:17 AM
Just use a printf statement to print out the line. You don't always need to pass something to the printf if all you are printing is a string. Use \n to create a carage return.
alvinleephd
02-10-2006, 12:24 AM
thanks a lot. would you happen to know how i can format it like so:
Payroll Program for X Y Z Corp.
Enter hours worked --> 10.0
Enter hourly rate --> 20.0
This employee worked 10.0 hours with a salary of $20 per hour. Here is the salary statement for this employee:
Gross pay: 200
Social security tax: -12
Federal tax: -20
State tax: -10
Medical/dental -10:
---------------------------------
Net pay: 148.00
_Aerospace_Eng_
02-10-2006, 12:27 AM
I don't see the problem here. Its all the same answer. Use the printf statement creating newlines where you need them by using \n.
alvinleephd
02-10-2006, 12:28 AM
oh, well ti just seems all cluttered up. i was wondering how to put spaces in between statements
i already figured out the line thing btw :) thanks
_Aerospace_Eng_
02-10-2006, 12:31 AM
You can have multiple newlines in the same statement.
printf("\n\n");
alvinleephd
02-10-2006, 12:37 AM
thanks! sorry for asking all these questions, it's all pretty new to me and i've had a bit of trouble.
how can i make it so the calculation can be repeated as often as the user wishes?
_Aerospace_Eng_
02-10-2006, 12:40 AM
Look into a do while loop. You will need to ask the user to enter a letter. Y to continue or N to quit. You will need to check to see if this letter matches Y or N. Note you will probably want to be checking for a char so you will be using %c in the scanf.
alvinleephd
02-10-2006, 12:48 AM
how would i put a do while loop?
also, how can i center or position the statements?
_Aerospace_Eng_
02-10-2006, 12:49 AM
Thats something you have to figure out. We aren't here to do your assignment for you. We are here to give you advice and on what you could do better. Read your book. It should be in there.
Good luck.
alvinleephd
02-10-2006, 01:03 AM
sorry, i looked in the book but i couldn't find a "do while" loop, but only a Do loop and While loop. i'm not particularly familiar with loops.
i only remember something my professor taught me when we created the average values program.. where the user could choose to add as many values as possible:
printf("\nWould you like to continue? (y/n) --> ")
get char(); /* ignore return character*/
scanf("%c",&answer);
could i apply this in any way to what i'm trying to do now?
_Aerospace_Eng_
02-10-2006, 01:17 AM
Yeah. Use the do loop. Put that printf statement inside of the do loop. You will want to start the do loop just before your first printf statement that tells the user to enter something. You will want to end it after the printf statement you posted. If you aren't required that you allow the user to enter more input then I wouldn't worry about it because at this point the concept of a loop is probably way out of your league.
alvinleephd
02-10-2006, 01:23 AM
does this look about right?
*/
#include <stdio.h>
int main ()
{
int x;
int y;
int Gross_Pay;
int Social_security_tax;
int Federal_tax;
int State_tax;
int Medical_dental;
int Net_Pay;
printf("Payroll Program for X Y Z Corp.\n");
printf("\n\n");
counter=0;
sum=0;
answer= 'y';
while (answer == 'y');
printf("Enter hours worked --> ");
scanf("%d", &x);
printf("Enter hourly rate --> ");
scanf("%d", &y);
printf("\n\n");
printf("This employee worked %d hours with a salary of $%d per hour. Here is the salary statement for this employee:\n",x,y);
printf("\n\n");
Gross_Pay=x*y;
printf("Gross Pay: %d\n", Gross_Pay);
Social_security_tax=x*y*.06;
printf("Social security tax: -%d\n", Social_security_tax);
Federal_tax=x*y*.1;
printf("Federal tax: -%d\n", Federal_tax);
State_tax=x*y*.05;
printf("State tax: -%d\n", State_tax);
Medical_dental=x*y*.05;
printf("Medical/dental: -%d\n", Medical_dental);
printf("----------------------------\n");
Net_Pay=x*y-(x*y*.06)-(x*y*.1)-(x*y*.05)-(x*y*.05);
printf("Net Pay: %d\n", Net_Pay);
printf("\nWould you like to repeat this process? (y/n) --> ");
get char(); /* ignore return character*/
scanf("%c",&answer);
return 0;
}
the code seems to be correct but i keep getting errors..
_Aerospace_Eng_
02-10-2006, 01:31 AM
No its not right. You need to put a { after while(blah) and put the closing } after scanf("%c", &answer);
You NEED to read your book. Not just look through it, actually read it and understand what it tells you. What book are you using anyways?
alvinleephd
02-10-2006, 01:34 AM
i believe i have those in place. though i still get an error of "unexpected ending"
this is what i have:
#include <stdio.h>
int main ()
{
int x;
int y;
int Gross_Pay;
int Social_security_tax;
int Federal_tax;
int State_tax;
int Medical_dental;
int Net_Pay;
char answer;
answer= 'y';
while (answer == 'y');
{
printf("Payroll Program for X Y Z Corp.\n");
printf("\n\n");
printf("Enter hours worked --> ");
scanf("%d", &x);
printf("Enter hourly rate --> ");
scanf("%d", &y);
printf("\n\n");
printf("This employee worked %d hours with a salary of $%d per hour. Here is the salary statement for this employee:\n",x,y);
printf("\n\n");
Gross_Pay=x*y;
printf("Gross Pay: %d\n", Gross_Pay);
Social_security_tax=x*y*.06;
printf("Social security tax: -%d\n", Social_security_tax);
Federal_tax=x*y*.1;
printf("Federal tax: -%d\n", Federal_tax);
State_tax=x*y*.05;
printf("State tax: -%d\n", State_tax);
Medical_dental=x*y*.05;
printf("Medical/dental: -%d\n", Medical_dental);
printf("----------------------------\n");
Net_Pay=x*y-(x*y*.06)-(x*y*.1)-(x*y*.05)-(x*y*.05);
printf("Net Pay: %d\n", Net_Pay);
{
printf("\nWould you like to repeat this process? (y/n) --> ");
scanf("%c",&answer);}
return 0;
}
_Aerospace_Eng_
02-10-2006, 01:36 AM
Take out that semi colon after while()
alvinleephd
02-10-2006, 01:38 AM
thanks SO much! it works perfectly now. though i had to tweak a few extra things.
you've been an AWESOME help! thanks again
alvinleephd
02-10-2006, 01:40 AM
ahh crap.. it seems to close after repeating the process once.
_Aerospace_Eng_
02-10-2006, 01:56 AM
You have an extra { here
{
printf("\nWould you like to repeat this process? (y/n) --> ");
scanf("%c",&answer);}
return 0;
alvinleephd
02-10-2006, 07:21 AM
thanks a lot! it works like a charm.
here's what i have:
#include <stdio.h>
int main ()
{
int x;
int y;
int Gross_Pay;
int Social_security_tax;
int Federal_tax;
int State_tax;
int Medical_dental;
int Net_Pay;
char answer;
answer= 'y';
while (answer == 'y')
{
printf("Payroll Program for X Y Z Corp.\n");
printf("\n\n");
printf("Enter hours worked --> ");
scanf("%d", &x);
printf("Enter hourly rate --> ");
scanf("%d", &y);
printf("\n\n");
printf("This employee worked %d hours with a salary of $%d per hour. Here is the salary statement for this employee:\n",x,y);
printf("\n\n");
Gross_Pay=x*y;
printf("Gross Pay: %d\n", Gross_Pay);
Social_security_tax=x*y*.06;
printf("Social security tax: -%d\n", Social_security_tax);
Federal_tax=x*y*.1;
printf("Federal tax: -%d\n", Federal_tax);
State_tax=x*y*.05;
printf("State tax: -%d\n", State_tax);
Medical_dental=x*y*.05;
printf("Medical/dental: -%d\n", Medical_dental);
printf("----------------------------\n");
Net_Pay=x*y-(x*y*.06)-(x*y*.1)-(x*y*.05)-(x*y*.05);
printf("Net Pay: %d\n", Net_Pay);
printf("\nWould you like to repeat this process? (y/n) --> ");
getchar(); /*ignore return character*/
scanf("%c",&answer);
}
return 0;
}
i have to write pseudocode for this program.. does this look okay?
1.)Declare variables
2.)Enter loop
3.)Ask user for values (hours worked and hourly rate)
4.)Read the first number(hours worked)
5.)Read the second number(hourly rate)
6.)Calculate gross pay = hours worked * hourly rate
7.)Calculate social security tax = hours worked * hourly rate*.06
8.)Calculate federal tax = hours worked * hourly rate*.1
9.)Calculate state tax = hours worked * hourly rate*.05
10.)Calculate medical/dental = hours worked * hourly rate*.05
11.)Calculate Net Pay = hours worked * hourly rate - (hours worked * hourly rate*.06) - (hours worked * hourly rate*.1) - (hours worked * hourly rate*.05) - (hours worked * hourly rate*.05)
12.) Enter end of loop, ask user if he/she wants to repeat
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.