Help with payroll code.

Hello, I'm very new to C++. I'm currently trying to figure this code out that calculates payroll with deductions and with-holdings. It's throwing a few errors that I don't quite understand and can't figure out. Any help is greatly appreciated!

#include <iostream>
#include <cmath>
using namespace std;
int main()

{
//Variables
double hours_worked;
int dependent, hours;
double sstax, fedinctax, stateinctax, uniondues, insurancedeps;
double grosspay, netpay, withholdings;


//Collect the required data from the employee

cout << "Enter the number of hours worked in a week: ";
cin >> hours_worked;

cout << "Enter the number of dependents you have: $";
cin >> insurancedeps;


//Calculations for the employee with a payrate of 16.78 per hour along with overtime pay of time and a half

if (hours_worked > 40)


grosspay = 16.78 * hours_worked +
(1.5) * (16.78) * (hours - hours_worked);
else
grosspay = 16.78 * hours;


//Calculations for the employees Social Security Tax, Federal and State Income Taxes, Uniondues, and Insurance Dependents

sstax = .06* grosspay;
fedinctax = .14 * grosspay;
stateinctax = .05 * grosspay;

//Calculations for if the employee has more than 3 dependents. If 3 or more than the additonal charge of $35

if (dependent >= 3.)
insurancedeps = 35;
else
insurancedeps = 0;
//Calculations for all of the employees withholdings and netpay

uniondues = 10;
withholdings = sstax + fedinctax + stateinctax + insurancedeps + uniondues;
netpay = grosspay - withholdings;

//Show all of the outputs

cout << "Gross pay = " << grosspay << endl;
cout << "Social Security Tax = " << sstax << endl;
cout << "Federal Income Tax = " << fedinctax << endl;
cout << "State Income Tax = " << stateinctax << endl;
cout << "Total Withholdings = " << withholdings << endl;
cout << "Net pay = " << netpay << endl;






return 0;
}
Your hours was never initialized to any value and then you tried to use it. That is the first problem. Can you fix that and see if you get any further errors or incorrect output?
Thank you! That was a simple fix. I have everything working for the most part. I'm having a trouble with overtime now. It seems to give me very large numbers when entering anything above 40 hours. Here's the new build:


#include <iostream>
#include <cmath>
using namespace std;
int main()

{
//Variables
double hours_worked, hourly_wage;
int dependent = 3;
double sstax, fedinctax, stateinctax, uniondues, insurancedeps;
double grosspay, netpay, withholdings;


//Collect the required data from the employee

cout << "Enter the number of hours worked in a week: ";
cin >> hours_worked;

cout << "Enter your hourly wage: ";
cin >> hourly_wage;

cout << "Enter the number of dependents you have: ";
cin >> dependent;


//Calculations for the employee with a payrate of 16.78 per hour along with overtime pay of time and a half

if (hours_worked > 40)


grosspay = hourly_wage * hours_worked +
(1.5) * (hourly_wage) * (hours_worked);
else
grosspay = hourly_wage * hours_worked;


//Calculations for the employees Social Security Tax, Federal and State Income Taxes, Uniondues, and Insurance Dependents

sstax = .06 * grosspay;
fedinctax = .14 * grosspay;
stateinctax = .05 * grosspay;

//Calculations for if the employee has more than 3 dependents. If 3 or more than the additonal charge of $35

if (dependent >= 3.)
insurancedeps = 35;
else
insurancedeps = 0;
//Calculations for all of the employees withholdings and netpay

uniondues = 10;
withholdings = sstax + fedinctax + stateinctax + insurancedeps + uniondues;
netpay = grosspay - withholdings;

//Show all of the outputs

cout << "Gross pay = " << grosspay << endl;
cout << "Social Security Tax = " << sstax << endl;
cout << "Federal Income Tax = " << fedinctax << endl;
cout << "State Income Tax = " << stateinctax << endl;
cout << "Total Withholdings = " << withholdings << endl;
cout << "Net pay = $ " << netpay << endl;




system("pause");

return 0;
}
you have your hours_worked multiplied twice. if your trying to get the over time then you should only be multiplying by the hours after 40.



if (hours_worked > 40)


grosspay = hourly_wage * hours_worked +
(1.5) * (hourly_wage) * (hours_worked);
else
grosspay = hourly_wage * hours_worked;

something like:

1
2
3
4
5
6
7
8
9
 int overTimeHours;
if(hours_worked > 40)
{
overTimeHours = hours_worked - 40;

grosspay = (hourly_wage * hours_worked) + (overTimeHours * 1.5);
}



Hope this Helps

Hello Elarton,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



When I compiled your program I received two errors telling me the variables hours and dependent are uninitialized. What this means is that you are trying to use these variables before you give them a value.

TheIdeasMan once wrote:
A golden rule: Always initialise your variables.

Something I agree with. From C++11 on you can use the uniform initializer of the {}s. An empty set will initialize a numeric variable to zero, a double to "0.0" and a char to "\0". You can also put a number inside the {}s to initialize to that number.A quoted string will initialize a "std::string".

Initializing the variables hours and dependent will eliminate the errors, but does nothing for the fact that you never give them a usable value to work with.

Looking at your code I see no need for the header file "cmath" in the program. Non of your calculations require "cmath".

The line grosspay = 16.78 * hours_worked + (1.5) * (16.78) * (hours - hours_worked);. The () around "1.5" and "16.78" have no real use because they are around a single magic number, i.e., there is no calculation for the () that say do this first.

The numbers "16.78" and "1.5" are considered magic numbers that should be avoided. One way to do this is to define constant variables to take their place. At the beginning of "main" you would define the variables:
1
2
3
constexpr double PAYRATE{ 16.78 };
constexpr double MAXREGHOURS{ 40 };
constexpr double OVERTIMERATE{ 1.5 };

Then the line would be written as:
grosspay = (PAYRATE * hours_worked) + ((OVERTIMERATE * PAYRATE) * (MAXREGHOURS - hours_worked));. The use of the constant variables makes it much easier to make changes because they are in one place and you do not have to hunt through the program to find where changes are to be made. This may seem like it is not needed in a program as small as yours, but consider a function with one or two hundred lines of code where a search and replace might change something you do not want to change.

Other numbers like "0.06", "0.14" and "0.05", which is the way they should be written, would also benefit being defined as a constant variable like above.

In the line if (dependent >= 3.). I do not know if "3." is a typo or intended, but what it is doing is comparing an "int" "dependent" to a "double" "3.0". In this case it did not flag any warning or error, but could be a potential problem. Also "dependent" has no usable value other than being initialized to zero when it was defined. So for now the if statement would never be true because "dependent" never receives a value. You should add a prompt and input a value before you reach this if statement.

I have not run the program yet, so if I find anything else I will let yo know.

Hope that helps,

Andy
Hello Elarton,

My apologizes I started to say that you should replace "cmath" with "iomanip" and add this line std::cout << std::fixed << std::showpoint << std::setprecision(2); before your "cout" statements in the "Show all of the outputs" section. The "showpoint" will print ".00" if it comes up and "setprecision(2)" will only print two decimal places.

Before I ran the program I noticed these lines:
1
2
cout << "Enter the number of dependents you have: $";
cin >> insurancedeps;

I think what you want is to input to "dependent" and later let the if statement set the value of "insurancedeps".

Before I ran the program I based the line that calculates the grose pay for anything over 40 hours based on what you started with. After running the program I found the calculation was incorrect it should be grosspay = (PAYRATE * hours_worked) + ((OVERTIMERATE * PAYRATE) * (hours_worked - MAXREGHOURS));. I had to switch that last part otherwise you could end up with a negative number.

With the changes I made when I tested the program with 50 hours worked it worked. I even checked the calculations on paper and they are correct.

One thing I did find is in your section "Show all of the outputs" you list everything except union dues,so it makes "Total Withholdings" look likee it is #10.00 more than what the other lines add up to. The only problem I found is that "Total Withholdings" rounds up to the next penny to make "netpay" appear to be one penny more than it should be. Not a big problem for this program, but something to consider for the future. Later I noticed that you do not have any output for what I believe would be "insurance" using the variable "insurancedeps ".

Hope this helps,

Andy

Edit:
Last edited on
Topic archived. No new replies allowed.