uninitialized local variable

Hello I am a beginner in C++ computer programming and have a program with the program. In a brief description I am taking an employee's of a fake company and computing his net pay. the problem is that in lines 141,143,147,148,149 it gives me this error. any help would be appreciated
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
***Algorithm?***
1. Declare Constants
1.1 FEDRATE, STATERATE, HOSPITAL, UNION
2. Get Data
2.1 Employee
2.2 Hours Worked
2.3 Hourly Rate
3. Calculation
3.1 TOTAL_WAGES = HRS_WORKED * HR_RATE
3.2 federalrate = TOTAL_WAGES * FEDRATE
3.3 staterate = TOTAL_WAGES * STATERATE
3.4 totaldeductions = federalrate + state rate + HOSPITAL + UNION
3.5 netpay = TOTAL_WAGES - totaldeductions
3.6 Netpay
3.6.1 sum the gross wages
4. Print Results for Employee (like in book)
5. repeat sets 1-4
6. Print summary report
*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;

void getData (string &name, double HRS_WORKED, double HR_RATE)
{
/*
data in: none
data out: employee name, hours worked, hourly rate
*/
cout << "enter name" << endl;
cin >> name;
cout << "enter hours worked" << endl;
cin >> HRS_WORKED;
cout << "enter hourly rate" << endl;
cin >> HR_RATE;
}
double calTotalwages(double HRS_WORKED, double HR_RATE)
{
/*
Data In: Total wage, Hours worked and Hourly rate
Data out: total wages
*/
double TOTAL_WAGES;
TOTAL_WAGES = HRS_WORKED * HR_RATE;
return TOTAL_WAGES;
}
double calFedrate( double FEDRATE, double TOTAL_WAGES)
{
/*
Data In: Total wages
Data Out: federal rate
*/
double federalTax;
federalTax = FEDRATE * TOTAL_WAGES;
return federalTax;
}
double calStaterate(double TOTAL_WAGES, double STATERATE)
{
/*
Data In: Total wages
Data Out: state withholding rate
*/
double stateTax;
stateTax = STATERATE * TOTAL_WAGES;
return stateTax;
}
double calHospital(double HOSPITAL)
{
/*
Data in: Hospital rates
Data out: Hospital rate
*/
double hospitalTax;
hospitalTax = HOSPITAL;
return hospitalTax;
}
double calUnionDues(double UNION)
{
/*
Data in: union amount
Data out: Union deductables\
*/
double unionTax;
unionTax = UNION;
return unionTax;
}
double caldeduction( double unionTax, double hospitalTax, double stateTax, double federalTax)
{
/*
Data in: union tax, hospital tax, state tax, ferdal tax
dat out: deductions
*/
double deduction;
deduction = unionTax + hospitalTax + stateTax + federalTax;
return deduction;
}
double calNetPay(double deduction, double TOTAL_WAGES)
{
/*
Data In: total pay - deduction
Data Out: Net Pay
*/
double netPay;
netPay = TOTAL_WAGES - deduction;
return netPay;
}
void printResults(double federalTax, double stateTax, double hospitalTax, double unionTax, double deduction, double netPay, string name, double HRS_WORKED, double HR_RATE, double TOTAL_WAGES)
{
/*
Data In: fed, state, hospital, union, deduction
Data Out: NONE
*/
	cout << "employee:" << name << endl;
	cout << "hours worked:" << HRS_WORKED << endl;
	cout << "hourly rate:" << HR_RATE << endl;
	cout << "total wages:" << TOTAL_WAGES << endl;
	cout << "____________________________" << endl;
	cout << "deductions:" << endl;
	cout << "federal withholding:" << federalTax << endl;
	cout << "state withholding:" << stateTax << endl;
	cout << "hospitalization:" << hospitalTax << endl;
	cout << "union dues" << unionTax << endl;
	cout << setw(15)<< "________" << endl;
	cout << "total deductions" << deduction << endl;
	cout << "net pay" << netPay << endl;
}
int main ()
{

const double FEDRATE = .18;
const double STATERATE = .045;
const double HOSPITAL = 25.65;
const double UNION = 7.85;

double HRS_WORKED, HR_RATE, total_wages, federal_tax, TOTAL_WAGES, state_tax, hospital_tax, union_dues, unionTax, hospitalTax, stateTax, federalTax, deductions, net_pay, deduction, netPay;
string name;

	getData(name, HRS_WORKED, HR_RATE);
	total_wages = calTotalwages(HRS_WORKED, HR_RATE);
	federal_tax = calFedrate(FEDRATE,TOTAL_WAGES);
    state_tax = calStaterate( TOTAL_WAGES, STATERATE);
    hospital_tax = calHospital( HOSPITAL);
	union_dues = calUnionDues(UNION);
    deductions = caldeduction(unionTax, hospitalTax, stateTax, federalTax);
    net_pay = calNetPay(deduction, TOTAL_WAGES);
    printResults(federalTax, stateTax, hospitalTax, unionTax, deduction, netPay, name, HRS_WORKED, HR_RATE, TOTAL_WAGES);

system ("pause");
return 0;
}
There are some problems/misunderstandings here.
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
int main ()
{

const double FEDRATE = .18;
const double STATERATE = .045;
const double HOSPITAL = 25.65;
const double UNION = 7.85;

double HRS_WORKED, HR_RATE, 
total_wages, //you also have TOTAL_WAGES - see below
federal_tax, 
TOTAL_WAGES, //already have total_wages - see above
state_tax, hospital_tax, union_dues, unionTax, hospitalTax, stateTax, federalTax, deductions, net_pay, deduction, netPay;
string name;

	getData(name, HRS_WORKED, HR_RATE);  //parameters HRS_WORKED and HR_RATE are passed by value - NOT correct when used in this way
//The getData function should be changed to pass by reference (like the way you pass the name)	
total_wages = calTotalwages(HRS_WORKED, HR_RATE); //notice here you use total_wages
	federal_tax = calFedrate(FEDRATE,TOTAL_WAGES); //now you start to use TOTAL_WAGES
    state_tax = calStaterate( TOTAL_WAGES, STATERATE);
    hospital_tax = calHospital( HOSPITAL);
	union_dues = calUnionDues(UNION);
    deductions = caldeduction(unionTax, hospitalTax, stateTax, federalTax); //Now you are using another set of unitialised variables - need to get your variables sorted out
    net_pay = calNetPay(deduction, TOTAL_WAGES);
    printResults(federalTax, stateTax, hospitalTax, unionTax, deduction, netPay, name, HRS_WORKED, HR_RATE, TOTAL_WAGES);

system ("pause");
return 0;
}


Some of the functions are pointless as they stand at the moment - for example the calHospital function is passed a value which it just simply returns.
1
2
3
4
5
6
7
8
9
10
double calHospital(double HOSPITAL)
{
/*
Data in: Hospital rates
Data out: Hospital rate
*/
double hospitalTax;
hospitalTax = HOSPITAL;
return hospitalTax;
}
Last edited on
Topic archived. No new replies allowed.