Payroll help!

Hi all,

I'm a programming student whose textbook is a worthless piece of crap (errors in the text). Google and forums such as this one are all that are getting me by, since the text has me entering code that doesn't compile. The course is online, and there are no lectures- just assigned readings.

Anyhow, I have an assignment where I have to build a payroll calculator and calculate various variables. Here are my issues:

1)The first grossPay outputs (lines 42 and 44) end up beginning with a zero. It didn't do that as I built that section of code.

2)The program doesn't recognize that lines 42 and 44 are supplying the number that becomes the variable "grossPay".

3)The taxation section produces accurate figures, but I don't know how to make the program realize that the state tax and federal tax sections are producing results that I want to fill the stateTax and federalTax variables, respectively.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{
                //declaring variables
                double hoursWorked;
                double payRate;
                double regularPay;
                double overtimePay;
                double grossPay;
                double stateTax;
                double federalTax;
                double totalTax;
                double unionDues;
                double netPay;
                
                regularPay = hoursWorked * payRate;
                overtimePay = 1.5 * payRate * hoursWorked;
                grossPay = regularPay + overtimePay;
                totalTax= federalTax + stateTax;
                netPay = grossPay - (totalTax + unionDues);

                char input;
                
                cout << "**************************************\n";
                cout << "          Payroll Calculator          \n";
                cout << "**************************************\n\n";    
                
                //Gross pay calculator      
                      
                cout << "Enter total hours \n" << endl;
                cin >> hoursWorked;
                cout << "\nEnter rate of pay \n" << endl;
                cin >> payRate;
                
                if ( hoursWorked > 40 )
                   cout << "Gross pay = " << grossPay << 1.5 * payRate * hoursWorked;
                else if( hoursWorked <=40 )
                    cout << "\nGross pay = " << grossPay << hoursWorked * payRate;
                
                //Taxation section
                
                cout << "\n\nTaxation section \n" << endl;                
                cout << "Please enter gross pay \n" << endl;
                
                cin >> grossPay;
                
                if ( grossPay < 500 )
                   cout << "State Tax is 0 \n" << endl;
                else
                    if (( grossPay >= 500 ) && ( grossPay < 1000 ))
                       cout << "State Tax is = " << grossPay * 0.03 << endl;
                    else
                        if ( grossPay >= 1000 )
                        cout << "State Tax is = " << grossPay * 0.05 << endl;
    
                        
                if ( grossPay < 500 )
                   cout << "Federal Tax is 0 \n" << endl;
                else
                    if (( grossPay >= 500 ) && ( grossPay < 1000 ))
                       cout << "Federal Tax is = " << grossPay * 0.05 << endl;
                    else
                        if ( grossPay >= 1000 )
                        cout << "Federal Tax is = " << grossPay * 0.07 << endl;
                        
                



                system("PAUSE");
                return 0;
}

Thanks so much for any advice you can give... hopefully I can return the favor once I learn a bit more.
Last edited on
1
2
3
4
5
regularPay = hoursWorked * payRate;
overtimePay = 1.5 * payRate * hoursWorked;
grossPay = regularPay + overtimePay;
totalTax= federalTax + stateTax;
netPay = grossPay - (totalTax + unionDues);


You are calculating with non-initialized values here.
In C++ you can only calculate when a variable is defined. Move these lines to after you get input for the variables in the equation.
Like the first one regularPay = hoursWorked * payRate; should be after cin >> payRate;

To set up federalTax and stateTax you need to include a initalizing statement in your if else's where you are outputting there values to the user. Like so:
1
2
3
4
5
if ( grossPay < 500 ) {
cout << "State Tax is 0 \n" << endl;
stateTax = 0.0;
}
else { //more statements 


EDIT:
You can shrink some of your repetitious couts for your State and Federal tax by assigning the value to them then printing it in one statement.

1
2
3
4
5
6
7
8
if (grossPay < 500) // A better if... else if statement. Note the else if, and the last statement is an else.
        stateTax = 0.0;
else if (grossPay >= 1000)
        stateTax = grossPay * 0.07;
else
        stateTax = grossPay * 0.05;

cout << "The State Tax is " << stateTax << endl;


EDIT2: ((I really need to check out this code fully before typing my replies and hitting submit))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if ( hoursWorked > 40 )
        grossPay = 1.5 * payRate * hoursWorked;
else // You don't need an else if here. else will cover the numbers less then 40.
        grossPay = hoursWorked * payRate;

cout << "Gross pay is: " << grossPay << endl;
// No need for cin, because grossPay is carried until the end of main()

cout << "\n\n"
        "****************************************\n"
        "            State & Federal Taxes       \n"
        "****************************************\n\n";
// Yes, this is a legal C++ statement. In compilation these will all be merged into one string for cout to print.
// Cin is not needed since grossPay is defined.
// ... code from above 

Last edited on
Put some [code][/code] tags around that next time.

Well, the problem appears to be that you don't completely understand that programming in C++ is sequential. For example, when you perform the assignments at the beginning, they are done right then; they are not "delayed" until you print the variable or something.

Since those variables contain garbage (you haven't initialized them), they results will most likely be garbage.
@wolfgang:

So those variables don't just carry down through the entire code?

I'm afraid I don't understand the initialization of the tax values...
Last edited on
@firedraco:

Code tags noted :)
I'm afraid I don't understand the initialization of the tax values...

You mean this?
1
2
3
4
5
6
7
8
9
if (grossPay < 500) 
        stateTax = 0.0; // the assignment is done if and only if  gross pay is 499 or less
else if (grossPay >= 1000)
        stateTax = grossPay * 0.07; // Another line of assignment. Fills the value of stateTax with the calculation to the right.
// For clarification the equal sign is done right to left, so all calculations on the right are done before any assignment to any value on the left.
else
        stateTax = grossPay * 0.05;

cout << "The State Tax is " << stateTax << endl; // Cout only prints the value. No assigning should happen here. 


Along with FireDraco's Comment, values can only be initialized by variables that have been already been initialized, and no assignment happens everywhere in the code. Those statements where you have them happen right then and there, and anything that is in variables before they are assigned (they have values, called GARBAGE. Its whatever the memory now allocated for those values had in them before being used for these values, interpreted as to their current type.) and then used in your calculations.

Best way to explain this is run this:
1
2
3
4
5
6
int two = 2; // defined and initalized
int three, one; // defined but NOT initalized
int four = two+two;
int six = two + three + one;

cout << "The value of four is " << four  << "\nand the value of six is " << six << endl;

And note the value of six which was made with uninitialized values
Last edited on
@Wolfgang

I love the code you've posted, but it gives my compiler errors (I'm running Dev C on Windows). I have similar issues with the code in my textbook... I don't know what is up with Dev.
DevC++ is no longer maintained and has a broken, outdated compiler. You should install wxDevC++ for a more modern compiler (with the same look and feel of DevC++) or install a better IDE like codeblocks, or Visual Studio Express.
Wolfgang,

I've got everything running the way I need now. I will look into alternate compilers. I now move on to other areas of the assignment, which I'm sure will necessitate my return here. You are a gentleman and a scholar, thank you.
Dev C is an outdated IDE. I'm not sure that that's why it won't compile, but regardless it's worth considering.

Give this a read:

http://www.cplusplus.com/articles/36vU7k9E/
Last edited on
In wxDevC++, when I go to run the code, no console window pops up, does anyone know what that's about?
You are clicking "Compile and Run" and have the file saved as a .cpp file?
I saved it via wx just in case... no dice.
Check the error log, see if it ran into a compile error and can't run the compiled file.
It ran with no errors. I've got the system("PAUSE"); code at the end also.
Okay, check the folder with the saved .cpp file and see if the executable is there.
Then open cmd.exe (use the Run Dialog to open) and navigate to the same folder using cd <path_to_folder>. Then run the .exe through cmd and see if it comes up. If that runs that way then there's something wrong with wxdev.
Topic archived. No new replies allowed.