C++ Coding on Displaying

I am trying to get the program to display empID - unionCode so the user can input certain data and then I also want the program to display regularPay-netPay with just data that was computed by the original information that the User put in those first 5 data entries

It had some errors in which told me assignment to "int" from "double", can anyone offer any guidance please?


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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
using namespace std;

int main ()
{

int empID; // Data in which user will input info that will be have arithmetic done on it and will be displayed 
cout << "empID ";
cin >> empID;

char payrollType;
cout << "payrollType ";
cin >> payrollType;

double hoursWorked;
cout << "hoursWorked ";
cin >> hoursWorked;

double payRate;
cout << "payRate ";
cin >> payRate ;

int unionCode;
cout << "unionCode ";
cin >> unionCode;

int regularPay; // Data here will merely be displayed as the user clicks enter after union code
cout << "regularPay is " << regularPay;

int overtimePay;
cout << "overtimePay is " << overtimePay;

int grossPay;
cout << "grossPay is " << grossPay;

int stateTax;
cout << "stateTax is " << stateTax;

int federalTax;
cout << "federalTax is " << federalTax;

int totalTax;
cout << "totalTax is " << totalTax;

int unionDues;
cout << "unionDues is " << unionDues;

int netPay;
cout << "netPay is " << netPay;

if (hoursWorked > 40) // If hours worked is > than 40, overtime commences
{
regularPay = 40 * payRate;
overtimePay = payRate * 1.5 * (hoursWorked - 40);
grossPay = regularPay + overtimePay;
}
else // If hours worked is < than 40, regular pay is only in effect
{
regularPay = hoursWorked * payRate;
overtimePay = 0;
grossPay = regularPay + overtimePay;
}

if  ( (grossPay >= 500) && (grossPay < 1000) ) // If gross pay is between 500-1000 3% is deduced
    {
    stateTax = grossPay * .03;
    }
else
    {
    stateTax = 0;
    }
if (grossPay > 1000) // If gross pay is > than 1000, 5% is deduced
    {
    stateTax = grossPay * .05;
    }
else
    {
    stateTax = 0;
    }


    if ((grossPay  >=  500) && (grossPay <= 1000)) // Federal Tax arithmetic
       {
       federalTax = grossPay * .05;
       }
    else
       {
       federalTax = 0;
       }
    if (grossPay > 1000)
       {
       federalTax = grossPay * .07;
       }
    else
       {
       federalTax = 0;
       }
{       
(totalTax = stateTax + federalTax) // totalTax and netPay arithmetic done wrong lol

(netPay = grossPay - (stateTax + federalTax + unionDues))
}


return 0;
}
I am very new to C++, but hopefully this answer is accurate enough to be useful:

I suggest using all double types instead of int types,
since that seems to fit with the calculations you are doing.

Anywhere you are assigning a double expression to an int will have the error you described.

For example, on line 53:
regularPay = 40 * payRate;

40 is type int
payRate is type double
regularPay is type int

The type of the (40 * payRate) expression will up convert to the largest type, in this case "double" because of payRate.
Then assigning that double result to the int payRate will cause an error due to the loss of precision.

You probably do not want the line below, but it would remove the error:
regularPay = (int)(40 * payRate);

That line is casting the double value to an int, but you will lose the decimal precision.

For example, the value of myVar below is "5", not 5.7.
int myVar = (int)5.7;

Using all double types instead will avoid loss of decimal precision.


ok, I revised my code, how do I implement those if/else statements correctly? Are they in the ok position or should they be moved elsewhere?

Also

So I compile and run and it goes up to regular pay which does the arithmetic like it should only except how do I get it to display regular pay without having the cin underneath it?

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
using namespace std;

int main ()
{

int empID; // Data in which user will input info that will be have arithmetic done on it and will be displayed 
cout << "empID ";
cin >> empID;

char payrollType;
cout << "payrollType ";
cin >> payrollType;

double hoursWorked;
cout << "hoursWorked ";
cin >> hoursWorked;

double payRate;
cout << "payRate ";
cin >> payRate ;

int unionCode;
cout << "unionCode ";
cin >> unionCode;

double regularPay = hoursWorked*payRate; 
cout << "regularPay " << regularPay;
cin >> regularPay;

double overtimePay;
cout << "overtimePay is " << overtimePay;
cin >> overtimePay;

double grossPay = regularPay + overtimePay;
cout << "grossPay is " << grossPay;

double stateTax;
cout << "stateTax is " << stateTax;

double federalTax;
cout << "federalTax is " << federalTax;

double totalTax = stateTax + federalTax;
cout << "totalTax is " << totalTax;

double unionDues;
cout << "unionDues is " << unionDues;

double netPay = grossPay - (stateTax + federalTax + unionDues);
cout << "netPay is " << netPay;

if (hoursWorked > 40) // If hours worked is > than 40, overtime commences
{
regularPay = 40 * payRate;
overtimePay = payRate * 1.5 * (hoursWorked - 40);
grossPay = regularPay + overtimePay;
}
else // If hours worked is < than 40, regular pay is only in effect
{
regularPay = hoursWorked * payRate;
overtimePay = 0;
grossPay = regularPay + overtimePay;
}

 if  ( (grossPay >= 500) && (grossPay <= 1000) ) // If gross pay is between 500-1000 3% is deduced
     {
     stateTax = grossPay * .03;
     }
 else
     {
     stateTax = 0;
     }
 if  (grossPay > 1000) // If gross pay is > than 1000, 5% is deduced
     {
     stateTax = grossPay * .05;
     }
 else
     {
     stateTax = 0;
     }


    if ((grossPay  >=  500) && (grossPay <= 1000)) // Federal Tax arithmetic
       {
       federalTax = grossPay * .05;
       }
    else
       {
       federalTax = 0;
       }
    if (grossPay > 1000)
       {
       federalTax = grossPay * .07;
       }
    else
       {
       federalTax = 0;
       }



return 0;
}
If I understood the question right, you would only need cin if you want to read input from the user.
You could just delete line 29 because regularPay is already assigned a value.

If you want to display all of the final values at the end, after the if/else, you could do:
1
2
3
4
5
cout << "Regular Pay: " << regularPay << '\n';
cout << "Overtime Pay: " << overtimePay << '\n';
cout << "Gross Pay: " << grossPay << '\n';
cout << "State Tax: " << stateTax << '\n';
cout << "Federal Tax: " << federalTax << '\n';


Your if/else statements look ok, but the grossPay blocks could be combined a bit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 if  ( (grossPay >= 500) && (grossPay <= 1000) ) // If gross pay is between 500-1000 3% is deduced
     {
     stateTax = grossPay * .03;
     federalTax = grossPay * .05;
     }
 else if  (grossPay > 1000) // If gross pay is > than 1000, 5% is deduced
     {
     stateTax = grossPay * .05;
     federalTax = grossPay * .07;
     }
else
     {
     stateTax = 0;
     federalTax = 0;
     }





This block should have the same effect.
Topic archived. No new replies allowed.