# Computer Science assignment- Payroll program.

Discussion in 'OT Technology' started by LALALALA, Feb 9, 2006.

1. ### LALALALAGuest

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:
Code:
```#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

2. ### k2737Active Member

Joined:
Nov 29, 2004
Messages:
3,324
0
Location:
Ft Lauderdale
Instead of printing out x, y then x*y in this line

printf("Gross Pay: %d * %d = %d\n", x, y, x * y);

just print out Gross_Pay

something like this:
printf("Gross Pay %d * %d = %d\n", Gross_Pay);

Its been awhile since I have programmed so the syntax might not be right.

Last edited: Feb 10, 2006
3. ### peerkNew Member

Joined:
Mar 14, 2005
Messages:
984
0
Yeah, you need to change the printf() statements.

printf() can look confusing if you don't know what is going on, but it isn't that bad once you figure it out.

The things with percent signs are replaced with their corresponding variable. And you list the variables after the quotes and separate them with commas.

%d means replace with an integer
%d means replace with a character
%s means replace with a string

Here are some examples:

Code:
```int favNumber = 7;
printf("My favorite number is %d.", favNumber);
```
This code will print out:

Example 2:

Code:
```int letter = 'a';
printf("%c is for apple.", letter);
```
This code will print out:

Example 3:

Code:
```int letter = 'a';
int favNumber = 7;

printf("I ate %d %cpples for lunch today.", favNumber, letter);
```
This code will print out:

Also in your code you have

Gross_Pay=x*y;
Social_security_tax=x*y*.06;

I would replace that with

Gross_Pay=x*y;
Social_security_tax= Gross_Pay * .06;

To me that makes the program easier to follow. And if you do that you will find calculations like net_Pay a lot more readable.

http://www.cplusplus.com/ref/cstdio/printf.html

Last edited: Feb 10, 2006
4. ### LALALALAGuest

thanks, but i already figured it out. this is what i came up with:

Code:
```#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");
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*/
}
return 0;
}```

5. ### LALALALAGuest

hmm.. i have to write pseudocode for this.

does this look ok?

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

6. ### StevesVR4Get Arrested

Joined:
Jul 1, 2003
Messages:
7,328
0
Could be better. Quit using "hours worked * hourly rate" once you calculate gross pay and use it instead. Also in the net pay calculation, replace all of the (hours worked * hourly rate * x) with the value you already calculated. It will make the program and pseudocode easier to read. Plus it will make your program run faster since you won't have to constantly multiply the hours by the rate. Of course with the pseudocode, you don't really need to include the equations.

7. ### FrequencyActive Member

Joined:
Dec 30, 2004
Messages:
7,503
0
Location:
PA
what ever happened to basic classes using cin and cout?

Joined:
Mar 29, 2003
Messages:
4,736