# Solved: payroll simple program in C++



## nubcoder (Feb 28, 2011)

Hello I am new to the forums and hope you all doing good.
I was assigned to make a payroll program that states the following:

"Plan and code a program to do the following> The Natural Oak and pine Furnitre has hired you to develop a payroll program to print out a pay report for one employee. ( later the program must be expanded for all comapny employes.)

Input Includes three employee initials; the number of hours worked; the hourly rate of pay for the employee. Calculate and output the employee's gross pay, deductions, and net pay, Deductions include the following

Federal WIthholding Rate 18 % gross wages
State Withholding rate 4.5 % of gross wages
Hospitalization $ 25.65
Union Dues: 2 % of gross wages.

Use constat defitions for the deductions. Out put employee, initials, gross wages, amount of federal withholding; amount of state withholding, amunt of hostpitalization; amount for union dues, total deductions; net wages, your output should be formatted as a report and may be similar to the following sample

Employee : ABC
Hours Worked: 40.00
Hourly Rate: 
Total Wages:

Deductions
Fedral Withholding:
State withholding:
Hospitalization:
union Dues

Total Deductions:
Net pay:

Turn in: Program Listing and program output
label all output clearly.

Note Be sure your putput file contains user prompts and what was entered by the user 
in addition to the results of your program processing...

...............
My algorithm was graded for my teacher so far it goes like this

*Get the initials of employee
*Get the nubmer of hours owrked for that employee
*Calculate the untaxed Total wages using the formula

Total Wages = Hours * Rate

*Find the Federal WIthholding Rate using the foruma

Fed WIthhold Rate = 0.18*Total Wages

*Find the State Wiholding Rate using the formula

State Withhold Rate = Total Wages * .045

*Find the hospitalization fee using the formula

Hospital Fee = Total Wages - 25.65

*calculate the Union Dues Fees Using the formula

Union Dues = Total Wages *.02

*Calculate the total deductions using the formula:

Total Deductions = FWR +SWR + Hospital + Union Fees )

*Calculate the net pay using the formula:

Net pay = Total Wages - Total Deductions..

Output has to look like this in a black dos screen

Employee:
Hours Worked:
Hourly Rate:
Total Wages:

*Deductions
Fed Withholding:
State Withholding:
Hospitalization fees:
Union Dues Fees

Total Deductions:
Total Pay:
... 
Finally my code is the following

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
int main()

{
string initials;

const double hourlyRate = 8,75; 
const double hoursworked = 40.00;
const double total wages = hourlyRate*hoursworked // ( rate*hourly )
const double FWRate = .18;
const double SWRate = .045;
const double Hospitalization = 23.65;
const double Union dues = .02

cout<<fixed<<showpoint<<setprecision(2);

string Initials;
cout<<"Enter initials\t\t"<<endl;
cin>>Initials;
cout<<"Hours Worked\t\t">>endl;
}

however I get like 11 errors, would anyone please point me out in the right direction? I am not asking you to do my homework just suggestion or anything will really help. I am really new into coding so i might have done something wrong I'll keep updating to see my payroll program goes.

TL;TR> Would you please point me out in the right direction


----------



## Ent (Apr 11, 2009)

I don't really do C, but here's my comments.

I'm sure this should have a dot rather than a comma.
const double hourlyRate = 8,75;

I'd suspect that you don't really want constants in some of those roles however. At some point you'd want your program to get the value of hoursworked and wage rate from a database or keyboard. (even under communist Russia not everyone has the same wages). It's better to have even values that don't change regularly (like the tax rate) set up so that they _can_ be changed without recompiling the entire program.


----------



## -Fabez- (Jul 28, 2008)

Your program had a few errors, which I have fixed for you. The revised code is shown below, with the errors and thier relevent fixes annotated by comments.


```
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iostream>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iomanip>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<string>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] std;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] _tmain(){[/SIZE]
[SIZE=2]string initials;[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//All of the const keywords can be removed so they can be changed in the program[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] hourlyRate = 8.75; [/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] hoursworked = 40.00;[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Spaces are not allowed in identifiers, so I removed the space and camel cased the identifier in variables that had this problem[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] totalWages = hourlyRate*hoursworked;[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//For a decimal smaller than 1 a leading 0 is still needed so I added 0 before the variables that had it missing[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FWRate = 0.18;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SWRate = 0.045;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Hospitalization = 23.65;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] UnionDues = 0.02;[/SIZE]
[SIZE=2]cout<<fixed<<showpoint<<setprecision(2);[/SIZE]
[SIZE=2]string Initials;[/SIZE]
[SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Enter initials\t\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<endl;[/SIZE]
[SIZE=2]cin>>Initials;[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//I changed the second set of symbols from '>>' to '<<' as the variable needs to go into the cout.[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Hours Worked\t\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<endl;[/SIZE]
[SIZE=2]}[/SIZE]
```


----------



## nubcoder (Feb 28, 2011)

-Fabez- said:


> Your program had a few errors, which I have fixed for you. The revised code is shown below, with the errors and thier relevent fixes annotated by comments.
> 
> 
> ```
> ...


Thanks a lot for the correction and great emphasis on the code you corrected, I appreciate your time, So far when I compile it it only shows as "Enter Initials and hours worked" however, I am struggling on how can I tell the compiler to perform the calculations such as total wages, based on the hours worked. so far my ideas would be something along the lines:

cout<<"totalWages"\t\t"<<endl;
cin>>hourlyRate*hoursworked;
cout<<"deductionst\

I am just not really wise enough yet on how to do this, help would be appreciated. Thanks in advance, love the forums allready, great help!


----------



## nubcoder (Feb 28, 2011)

Ent said:


> I don't really do C, but here's my comments.
> 
> I'm sure this should have a dot rather than a comma.
> const double hourlyRate = 8,75;
> ...


yes this is the exact problem I am having, I cant get the compiler to do the math as shown on my algorithm, I appreciate your time and concern about my problem. Unfortunely, my teacher only want us to work from information that is in the code and ask the user and then perform the calculations based on the user information without the need of a data file. I do see your point thou that bringing up a data file is what you'd perhaps be used to see in a payroll program, however we are all noobie coders thus we are only studying the abcs of coding for now.

best regards: nubcoder


----------



## -Fabez- (Jul 28, 2008)

It's good to see you found the code helpfull. If you are evaluating expressions in a cout statement it is generally a good idea to enclose it in brackets. I have written you another piece of code, is a working example of what you want, with some comments added in again. If there is anything you are uncertain of or would like clarifying, feel free to ask.


```
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] _tmain(){[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Variable Declarations[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//I reread the given scenario and if your tutor wants you to use const, it's best to leave them in[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]string EmployeeInitials;[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//As these are set later in the program I have declared them all on one line[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] HoursWorked, HourlyRate, TotalPay, Deductions;[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//These are given values when created so they are declared on separate lines[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]const [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FederalWithholding = 0.18;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]const [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] StateWithholding = 0.045;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]const [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] UnionDues = 0.02;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]const [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Hospitalisation = 25.65;[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//This code is basically the same as the code you currently have[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Employee Intials: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]cin >> EmployeeInitials;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"How Many Hours Did They Work ?: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]cin >> HoursWorked;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"What Is Thier Hourly Pay ?: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]cin >> HourlyRate;[/SIZE]
 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Work out the employees total pay and store it so we don't have to keep recalculating it[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]TotalPay = (HoursWorked * HourlyRate);[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Once again, this code is basically the same as the code you currently have[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Endl is the equivalent of a new line[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//All of the calculations that are in cout statements have been put into (brackets) so that they are evaluated[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]cout << endl << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Employee: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << EmployeeInitials << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Hours Worked: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << HoursWorked << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Hourly Pay: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << HourlyRate << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Total Pay: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << TotalPay << endl;[/SIZE]
[SIZE=2]cout << endl << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Deductions"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << endl << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"----------"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Federal Withholding: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << (TotalPay * FederalWithholding) << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"State Withholding: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << (TotalPay * StateWithholding) << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Union Dues: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << (TotalPay * UnionDues) << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Hospitalisation: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << Hospitalisation << endl << endl;[/SIZE]
 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Work out the employees total deductions and store it so we don't have to keep recalculating it[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//This formula works out the total percent to be subtracted, subtracts that from the employees pay, and then deducts the set hospitalisation fee[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]Deductions = (TotalPay * (FederalWithholding + StateWithholding + UnionDues)) + Hospitalisation;[/SIZE]
 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Output their final pay & deductions[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Total Deductions: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << Deductions << endl;[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Final Pay: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << (TotalPay - Deductions) << endl << endl;[/SIZE]
 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Passes the pause command to the console to make it stay open[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]}[/SIZE]
```
-Fabez-


----------



## nubcoder (Feb 28, 2011)

-Fabez- said:


> It's good to see you found the code helpfull. If you are evaluating expressions in a cout statement it is generally a good idea to enclose it in brackets. I have written you another piece of code, is a working example of what you want, with some comments added in again. If there is anything you are uncertain of or would like clarifying, feel free to ask.
> 
> 
> ```
> ...


Thanks a lot for the great help Febez, currently I am working on it so it looks aligned I almost got it with the line of code "cout<<fixed<<showpoint<<setprecision(2);" you been greater help than my own teacher the constant and doubles were giving me syntax errors but i fixed those right away. the program is working fair and square, I admire your dedication and fast response to newcomers, will definitely trouble you with my new finds to come!

Best Regards:
nubcoder


----------



## -Fabez- (Jul 28, 2008)

nubcoder said:


> Thanks a lot for the great help Febez, currently I am working on it so it looks aligned I almost got it with the line of code "cout<<fixed<<showpoint<<setprecision(2);" you been greater help than my own teacher the constant and doubles were giving me syntax errors but i fixed those right away. the program is working fair and square, I admire your dedication and fast response to newcomers, will definitely trouble you with my new finds to come!


It's good that your problem regarding your assignment is now sorted & Thanks for the compliments, it's nice to see it's appreciated  If you have any other questions or need help on anything else, feel free to post it here.

-Fabez-


----------

