Question regarding my code.

Ok, So I've exhausted every other resource before coming to this forum for help. I have a program that I am writing for my class. The program checks the net pay (not sure why the instructor did not used the gross pay) and if it is less than or equal to 2,000 apply a 7% tax, if its greater than 2,000 but less or equal to 17,000 apply a 10%, if its greater than 17,000 but less than or equal to 30,000 apply a 15%, if its greater than 30,000 but less than or equal to 50,000 apply a 28% and if its greater than 50,000 apply a 33%. The program must ask for the tax payer's name, tax year, account number and the net pay in question. I started writing the code as functions but for some reasons I'm having 0 return in my contributions part. Hopefully someone can take a look at it for me and give me an idea of where my issue is. I'm guessing it is a " &" issue or probably I'm having a local variable where I should probably have a global one ( I know I have to modify my output with setw's and all, just want to have it run tho...).

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
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <cstring>
#include <iomanip>

//no constants in this lab

using namespace std;

double contribution;
//PROTOTYPES

void obtain_worker_values (char [80], char [80], char [80], double &);
void calc_contribution( double &, double &, double &, double);
void disp_report_to_worker(char [80], char [80],
    char [80], double , double );


int main(void) 

{
   char worker_name [80], contribution_yr [80], acc_num [80];
    
   double netpay, contribution, contribution1, netPay;
    
    
//CALLS   
    
//Obtain Worker's Information
obtain_worker_values (worker_name, contribution_yr, acc_num, netpay);

//Calculate Contribution
calc_contribution (netpay, contribution, netPay, contribution1);



//disp_report_to_worker
disp_report_to_worker(worker_name, contribution_yr, acc_num, netPay, 
        contribution1);
    
    return 0;
}

//FUNCTIONS

//set student values
void obtain_worker_values( char worker_name [80], char contribution_yr [80],
    char acc_num [80], double &netpay)

{
    cout << "Enter worker's name: ";
    cin.getline(worker_name,80);
    cout << "Enter the contribution year: ";
    cin.getline(contribution_yr,80);
    cout << "What is your account number?: ";
    cin.getline (acc_num,80);
    cout << "Enter your Net Pay: ";
    cin >> netpay;
    cout << endl;
}

//calc assignment  average
void calc_contribution( double &netpay, double &contribution, double &netPay,
                        double taxrate)
{  
    contribution = 0;
    
    if (netpay <= 2000)
    {
        taxrate =  0.07;    
    }
    else if (netpay > 2000 || netpay <= 17000)
    {    
        taxrate =  0.10;
    }
    else if (netpay > 17000 || netpay <= 30000)
    {    
        taxrate =  0.15;
    }
    else if (netpay > 30000 || netpay <= 50000)
    {    
        taxrate = 0.28;
    }
    else if (netpay > 50000)
    {    
        taxrate = 0.33;
    }
    netPay = netpay;
    contribution = netPay * taxrate;
    cout << endl << endl;
    
}

//disp student scores to student
void disp_report_to_worker(char worker_name [80], char contribution_yr [80],
    char acc_num [80], double netPay, double contribution)
{
    cout << "********************* Start Report ************************" 
         << endl;
    cout << "*                                                         *" 
         <<endl;
    cout << "*                     Net Pay Report                      *" 
         << endl;
    cout << "* Worker's Name: " <<worker_name << "                            *"
         << endl;
    cout << "* Contribution Year: " << contribution_yr << "                   *" 
         << endl;
    //cout << "* Worked Hours: " << workHours << "                            "
//         << "            *"   << endl;
    cout << "* Account Number: " << acc_num 
         << "                                     *"  << endl;
    cout << "* Net Pay: " << netPay 
        << "                                    *" << endl;
    cout << "* Contribution: " << contribution 
         <<"                                    *" <<endl;    
    cout << "*******************  End Report ***************************";
    cout << "\n\nDone\n\n";
1
2
//Calculate Contribution
calc_contribution (netpay, contribution, netPay, contribution1);


1
2
3
4
//calc assignment  average
void calc_contribution( double &netpay, double &contribution, double &netPay,
                        double taxrate)
{  


I see confusion brewing there :+) And it manifests itself here:

In function 'int main()': 32:64: warning: 'contribution1' is used uninitialized in this function [-Wuninitialized] 


37
38
39
//disp_report_to_worker
disp_report_to_worker(worker_name, contribution_yr, acc_num, netPay, 
        contribution1);


Now I found this problem by compiling with cpp.sh (the gear icon top right) with all 3 warning levels turned on. So what warning levels do you have? Not enough I imagine :+) I just mention this so you can get your compiler to help you out.

With g++ compiler I use these as a minimum:

g++ -std+c++14 -Wall -Wextra -pedantic-errors -o ExecutableName *.cpp

Good Luck !!

Last edited on
Ok. I thought I Had initialized all my variables and what not but here is a corrected one. still having the same issue...

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
107
108
109
110
111
112
113
114
115
116
117
 #include <iostream>
#include <cstring>
#include <iomanip>

//no constants in this lab

using namespace std;

double contribution;
//PROTOTYPES

void obtain_worker_values (char [80], char [80], char [80], double &);
void calc_contribution( double &, double &, double &, double);
void disp_report_to_worker(char [80], char [80], char [80], double, double);


int main(void) 

{
   char worker_name [80], contribution_yr [80], acc_num [80];
    
   double netpay, contribution, netPay, taxrate;
    
    
//CALLS   
    
//Obtain Worker's Information
obtain_worker_values (worker_name, contribution_yr, acc_num, netpay);

//Calculate Contribution
calc_contribution (netpay, contribution, netPay, taxrate);



//disp_report_to_worker
disp_report_to_worker (worker_name, contribution_yr, acc_num, netPay, contribution);
    
    return 0;
}

//FUNCTIONS

//set student values
void obtain_worker_values( char worker_name [80], char contribution_yr [80],
    char acc_num [80], double &netpay)

{
    cout << "Enter worker's name: ";
    cin.getline(worker_name,80);
    cout << "Enter the contribution year: ";
    cin.getline(contribution_yr,80);
    cout << "What is your account number?: ";
    cin.getline (acc_num,80);
    cout << "Enter your Net Pay: ";
    cin >> netpay;
    cout << endl;
}

//calc assignment  average
void calc_contribution( double &netpay, double &contribution, double &netPay,
                        double taxrate)
{  
    contribution = 0;

    
    if (netpay <= 2000)
    {
        taxrate =  0.07;    
    }
    else if (netpay > 2000 || netpay <= 17000)
    {    
        taxrate =  0.10;
    }
    else if (netpay > 17000 || netpay <= 30000)
    {    
        taxrate =  0.15;
    }
    else if (netpay > 30000 || netpay <= 50000)
    {    
        taxrate = 0.28;
    }
    else if (netpay > 50000)
    {    
        taxrate = 0.33;
    }
    netPay = netpay;
    contribution = netPay * taxrate;
    cout << endl << endl;
    
}

//disp student scores to student
void disp_report_to_worker(char worker_name [80], char contribution_yr [80],
    char acc_num [80], double netPay, double contribution)
{
    cout << "********************* Start Report ************************" 
         << endl;
    cout << "*                                                         *" 
         <<endl;
    cout << "*                     Net Pay Report                      *" 
         << endl;
    cout << "* Worker's Name: " <<worker_name << "                            *"
         << endl;
    cout << "* Contribution Year: " << contribution_yr << "                   *" 
         << endl;
    //cout << "* Worked Hours: " << workHours << "                            "
//         << "            *"   << endl;
    cout << "* Account Number: " << acc_num 
         << "                                     *"  << endl;
    cout << "* Net Pay: " << netPay 
        << "                                    *" << endl;
    cout << "* Contribution: " << contribution 
         <<"                                    *" <<endl;    
    cout << "*******************  End Report ***************************";
    cout << "\n\nDone\n\n";
}
 
What about this warning?
In function 'int main()':
31:58: warning: 'taxrate' is used uninitialized in this function [-Wuninitialized]


It's what I'm working on right now. I have declared the variable.. and i made taxrate = 0; on my program at home and it's still spitting the same warning..
i made taxrate = 0; on my program at home and it's still spitting the same warning..

I don't know if this "program at home" is the same one as your last post but look at this snippet:

1
2
3
4
5
6
7
8
9
10
int main(void) 

{
   char worker_name [80], contribution_yr [80], acc_num [80];
    
   double netpay, contribution, netPay, taxrate;
...

//Calculate Contribution
calc_contribution (netpay, contribution, netPay, taxrate);


Where in this snippet do you assign a value to taxrate? And why are you passing this variable, by value, into the function? It looks like it should just be a variable that is local to the function.

Topic archived. No new replies allowed.