Displaying Variables in the End / Arithmetic

How do I get to display the variables (many of them which will have arithmetic done on them and have yet to write the code on that) at the end so the user could see all his totals?

#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 at the end
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;

float regularPay = 0.0; // Initializing all variables I will use/display
float overtimePay = 0.0;
float grossPay = 0.0;
float stateTax = 0.0;
float federalTax = 0.0;
float totalTax = 0.0;
float unionDues = 0.0;
float netPay = 0.0;

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<=1000) // If gross pay is between 500-1000m 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;


{ // Weak attempt to try to display all the info at the end...
cout << "empID " << empID << "!\n\n";
cout << "payrollType " << payrollType << "!\n\n";
cout << "hoursWorked " << hoursWorked << "!\n\n";
cout << "payRate " << empID << "!\n\n";
cout << "regularPay " << regularPay << "!\n\n";
cout << "overtimePay " << overtimePay << "!\n\n";
cout << "grossPay " << grossPay << "!\n\n";
cout << "stateTax " << stateTax << "!\n\n";
cout << "federalTax " << federalTax << "!\n\n";
cout << "totalTax " << totalTax << "!\n\n";
cout << "unionDues " << unionDues << "!\n\n";
cout << "netPay " << netPay << "!\n\n";
}

return 0;
}
From the top:

C++ does not recognise If, only if.
C++ does not recognise Else, only else.

This condition is nonsensical:

(grossPay =>500<=1000)

and also => does not exist in C++, only >=

% is the modulus operator and has no place where you are putting it. You can only multiply numbers and variables together; what do you expect to happen when you try to multiply something by the % operator?

Take your cout block out of those braces ( i.e. remove the { and } that surround them).

Indent your code properly so you can see easily that you've got a problem with how many braces you have in your code. You have eight opening braces and only seven closing braces.
Last edited on
Thanks for the reply, I updated my code to look cleaner and once I compile / run it, it still does not display all the variables in the end...?

#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 at the end
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;

float regularPay = 0.0; // Initializing all variables I will use/display
float overtimePay = 0.0;
float grossPay = 0.0;
float stateTax = 0.0;
float federalTax = 0.0;
float totalTax = 0.0;
float unionDues = 0.0;
float netPay = 0.0;

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<1000) // If gross pay is between 500-1000m 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;


// Weak attempt to try to display all the info at the end... :(
cout << "empID " << empID << "!\n\n";
cout << "payrollType " << payrollType << "!\n\n";
cout << "hoursWorked " << hoursWorked << "!\n\n";
cout << "payRate " << empID << "!\n\n";
cout << "regularPay " << regularPay << "!\n\n";
cout << "overtimePay " << overtimePay << "!\n\n";
cout << "grossPay " << grossPay << "!\n\n";
cout << "stateTax " << stateTax << "!\n\n";
cout << "federalTax " << federalTax << "!\n\n";
cout << "totalTax " << totalTax << "!\n\n";
cout << "unionDues " << unionDues << "!\n\n";
cout << "netPay " << netPay << "!\n\n";


return 0;
}
Still got the wrong number of braces, so I had to add one to make it compile; it compiled, and runs, and gives output. Take a look at where your cout statements are. With the code as above in your last post, will the cout statements always be reached?

I'm surprised you can get it to compile with a missing brace.


grossPay >=500<1000

I don't think you understand how conditions work in C++. This is almost certainly not saying what you think it is. I expect you mean this:

( (grossPay >= 500) && (grossPay < 1000) )
Last edited on
No, im trying to make it to where if its equal or greater than 500 but equal and less than 1000, don't know the condition for that although im learning a lot as you guys input which I really do appreciate it, fixed the brace lol and know it works but it only outputs the beginning data items but it does not output them once again after the user inputted the data and the program ran the arithmetic on it...
In the code you have above, your cout statement are part of your last else block, so they will only be reached if you enter that else block. I suspect you meant to have a closing brace after stateTax = 0;
Last edited on
Newly revised source code. I put the closing brace after stateTax and when I compile and run, it only outputs the beginning data such as empID, payrollType, hoursWorked, payRate, and unionCode. Yet my goal is to have the user input the variables in each data item and have the program run arithmetic to figure out some of the missing variables like federalTax and netPay which I am working on as we speak to create the code... And then in the end, to have the program display all the data info from empID to netPay... Once again, highly appreciate the help guys, I am learning a lot.

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
#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 at the end
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;

    float regularPay = 0.0; // Initializing all variables I will use/display
    float overtimePay = 0.0;
    float grossPay = 0.0;
    float stateTax = 0.0;
    float federalTax = 0.0;
    float totalTax = 0.0;
    float unionDues = 0.0;
    float netPay = 0.0;

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;
    }


         // Weak attempt to try to display all the info at the end... :(
         cout << "empID " << empID << "!\n\n";
         cout << "payrollType " << payrollType << "!\n\n";
         cout << "hoursWorked  " << hoursWorked  << "!\n\n";
         cout << "payRate   " << empID << "!\n\n";
         cout << "regularPay " << regularPay << "!\n\n";
         cout << "overtimePay " << overtimePay << "!\n\n";
         cout << "grossPay " << grossPay << "!\n\n";
         cout << "stateTax " << stateTax << "!\n\n";
         cout << "federalTax " << federalTax << "!\n\n";
         cout << "totalTax " << totalTax << "!\n\n";
         cout << "unionDues " << unionDues << "!\n\n";
         cout << "netPay " << netPay << "!\n\n";


return 0;
}
it only outputs the beginning data such as empID, payrollType, hoursWorked, payRate, and unionCode.


Are you saying that you think it should now be outputting some other data as well, or are you just saying that when you're finished it will do more?
All I want is when I run the program, for it to output the beginning data such as empID, payrollType, hoursWorked, payRate, and unionCode. Then, after the user finishes inputting all the info on each slot, I'd like it to (with the help of me putting in more arithmetic codes) to display all of what is on the big section of cout in the end of my coding (empID - netPay)...




Last edited on
does anyone have any idea what im talking about?
So how can I get the rest of the data items to show in the beginning so as to start displaying stateTax, federalTax, netPay, after the user inputs the information?

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
#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 at the end
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;

    float regularPay = 0.0; // Initializing all variables I will use/display
    float overtimePay = 0.0;
    float grossPay = 0.0;
    float stateTax = 0.0;
    float federalTax = 0.0;
    float totalTax = 0.0;
    float unionDues = 0.0;
    float netPay = 0.0;

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))
       {
       federalTax = grossPay * .05;
       }
    else
       {
       federalTax = 0;
       }
    if (grossPay > 1000)
       {
       federalTax = grossPay * .07;
       }
    else
       {
       federalTax = 0;
       }
{       
(totalTax = stateTax + federalTax)
}
{
(netPay = grossPay - (stateTax + federalTax + unionDues))
}

         // Weak attempt to try to display all the info at the end... :(
         cout << "empID " << empID << "!\n\n";
         cout << "payrollType " << payrollType << "!\n\n";
         cout << "hoursWorked  " << hoursWorked  << "!\n\n";
         cout << "payRate   " << empID << "!\n\n";
         cout << "regularPay " << regularPay << "!\n\n";
         cout << "overtimePay " << overtimePay << "!\n\n";
         cout << "grossPay " << grossPay << "!\n\n";
         cout << "stateTax " << stateTax << "!\n\n";
         cout << "federalTax " << federalTax << "!\n\n";
         cout << "totalTax " << totalTax << "!\n\n";
         cout << "unionDues " << unionDues << "!\n\n";
         cout << "netPay " << netPay << "!\n\n";


return 0;
}
Topic archived. No new replies allowed.