Error C2062, C2065, C2086

Hi I need help, creating this c++ program Modular Programs, I keep getting on error in line 11 and 23 error C2062 float unexpected and line 42 , 43 undeclared indentifier C2065, here's my code

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const float FEDTAX_RATE = 0.18, STATETAX_RATE = 0.045,   //error C2062 float unexpected
const float HOSPITALIZATION = 25.65, UNION_DUES = 0.02;   //error C2062
void GetData(string &, float &, float &, bool, bool);
void Calc(float, float, float&, float&, float&, float&, float&, float&, float&, float&);
void SendData(string, float, float, float, float, float, float, float, float,float);
ifstream infile;
ofstream outfile, errorfile;

int main() {
	string Name;
	int i, j, row = 10, col = 8;
	float HoursWorked, HourlyRate;
	float TotalHoursWorked, GrossPay,
	float FedW, StateW, Union, Hospital, TotalDeductions, NPay;
	bool RateError, HoursError;
	string Name;

	infile.open("Payln.txt");
	errorfile.open("FileError.txt");
	outfile.open("PayOut.txt");
	if (!outfile || !errorfile)
	{
		cout << "Cannot write the file, please open the Output file" << endl;
		cout << "program terminates..." << endl;
		exit(1);
	}
	infile >> Name >> HoursWorked >> HourlyRate;
	while (!infile.eof()) 
	{
		GetData(Name, HoursWorked, HourlyRate, HoursError, RateError);
		if (!HoursError && !RateError)
		{
			Calc(HoursWorked, HourlyRate, TotalHoursWorked, GrossPay, FedW, StateW, Union, Hospital, TotalDeductions, NPay);   //error undeclared identifier
			SendData(Name, TotalHoursWorked, HourlyRate, GrossPay, FedW, StateW, Union, Hospital, TotalDeductions, NPay);   //error undeclared identifier
		}
	}    // end while

infile >> Name >> HoursWorked >> HourlyRate;
infile.close();
outfile.close();
errorfile.close();
return 0;
}  // main end braces

// read data from the files and checking errors
void GetData(string &Name, float &HoursWorked, float &HourlyRate, bool HoursError, bool RateError)
{
	infile >> Name >> HoursWorked >> HourlyRate; 
	HoursError = 25.0;
	RateError < 0.0;
	if (HoursError)
		errorfile << "Error data, should be at least 24 hours:" << Name << " " << HoursWorked << " " << HourlyRate << endl;
	if (RateError)
		errorfile << "Error data, regulars rate is $8.75: " << Name << " " << HoursWorked << " " << HourlyRate << endl;
}
// Calculate total hours worked, grosspay, deductions and netpay
void Calc(float HoursWorked, float HourlyRate, float &TotalHoursWorked, float &GrossPay, float &FedWithholding, float &StateWithholding, float &UnionDues, float & Hospitalization, float &Deductions, float &NetPay)
{
	TotalHoursWorked = TotalHoursWorked + HoursWorked;
	GrossPay = TotalHoursWorked * HourlyRate;
	FedWithholding = FEDTAX_RATE * GrossPay;
	StateWithholding = STATETAX_RATE * GrossPay;
	UnionDues = UNION_DUES * GrossPay;    //error undeclared identifier
	Hospitalization = HOSPITALIZATION;          //error undeclared identifier
	Deductions = FedWithholding + StateWithholding + UnionDues + Hospitalization;
	NetPay = GrossPay - Deductions;
}
// Output the valid data to PayOut.txt file
void SendData(string Name, float TotalHoursWorked, float HourlyRate, float GrossPay, float FedWithholding, float StateWithholding, float UnionDues, float Hospitalization, float Deductions, float NetPay)
{
	outfile << fixed << showpoint << setprecision(2);
	outfile << "Employees Initials  : " << Name << endl;
	outfile << "Total Hours Worked  : " << TotalHoursWorked << endl;
	outfile << "Hourly Rate Pay     : " << HourlyRate << endl;
	outfile << "Gross Pay			: " << GrossPay << endl;
	outfile << "Federal Withholding : " << FedWithholding << endl;
	outfile << "State Withholding   : " << StateWithholding << endl;
	outfile << "Union Dues          : " << UnionDues << endl;
	outfile << "Hospitalization     : " << Hospitalization << endl;
	outfile << "Total Deductions    : " << Deductions << endl;
	outfile << "Net Pay             : " << NetPay << endl;
}
Last edited on
I place the comments inside the code for the correction to the compiler errors
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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const float FEDTAX_RATE = 0.18, STATETAX_RATE = 0.045; //Was missing semicolon
const float HOSPITALIZATION = 25.65, UNION_DUES = 0.02; 
void GetData(string &, float &, float &, bool, bool);
void Calc(float, float, float&, float&, float&, float&, float&, float&, float&, float&);
void SendData(string, float, float, float, float, float, float, float, float, float);
ifstream infile;
ofstream outfile, errorfile;

int main() {
    string Name;
    int i, j, row = 10, col = 8;
    float HoursWorked, HourlyRate;
    float TotalHoursWorked, GrossPay; // Was missing semicolon.
    float FedW, StateW, Union, Hospital, TotalDeductions, NPay;
    bool RateError = true, HoursError = true; // Example on defining the variables to be used on function GetData (Line 35).
    //string Name; This variable is already declared on line 15.

    infile.open("Payln.txt");
    errorfile.open("FileError.txt");
    outfile.open("PayOut.txt");
    if (!outfile || !errorfile)
    {
	   cout << "Cannot write the file, please open the Output file" << endl;
	   cout << "program terminates..." << endl;
	   exit(1);
    }
    infile >> Name >> HoursWorked >> HourlyRate;
    while (!infile.eof())
    {
	   GetData(Name, HoursWorked, HourlyRate, HoursError, RateError); // Need to define the variables HoursError and RateError (Line 20).
	   if (!HoursError && !RateError)
	   {
		  Calc(HoursWorked, HourlyRate, TotalHoursWorked, GrossPay, FedW, StateW, Union, Hospital, TotalDeductions, NPay);
		  SendData(Name, TotalHoursWorked, HourlyRate, GrossPay, FedW, StateW, Union, Hospital, TotalDeductions, NPay);
	   }
    } // end while

    infile >> Name >> HoursWorked >> HourlyRate;
    infile.close();
    outfile.close();
    errorfile.close();
    return 0;
} // main end braces

// read data from the files and checking errors
void GetData(string &Name, float &HoursWorked, float &HourlyRate, bool HoursError, bool RateError)
{
    infile >> Name >> HoursWorked >> HourlyRate;
    HoursError = 25.0;
    RateError < 0.0;
    if (HoursError)
	   errorfile << "Error data, should be at least 24 hours:" << Name << " " << HoursWorked << " " << HourlyRate << endl;
    if (RateError)
	   errorfile << "Error data, regulars rate is $8.75: " << Name << " " << HoursWorked << " " << HourlyRate << endl;
}
// Calculate total hours worked, grosspay, deductions and netpay
void Calc(float HoursWorked, float HourlyRate, float &TotalHoursWorked, float &GrossPay, float &FedWithholding, float &StateWithholding, float &UnionDues, float & Hospitalization, float &Deductions, float &NetPay)
{
    TotalHoursWorked = TotalHoursWorked + HoursWorked;
    GrossPay = TotalHoursWorked * HourlyRate;
    FedWithholding = FEDTAX_RATE * GrossPay;
    StateWithholding = STATETAX_RATE * GrossPay;
    UnionDues = UNION_DUES * GrossPay; 
    Hospitalization = HOSPITALIZATION; 
    Deductions = FedWithholding + StateWithholding + UnionDues + Hospitalization;
    NetPay = GrossPay - Deductions;
}
// Output the valid data to PayOut.txt file
void SendData(string Name, float TotalHoursWorked, float HourlyRate, float GrossPay, float FedWithholding, float StateWithholding, float UnionDues, float Hospitalization, float Deductions, float NetPay)
{
    outfile << fixed << showpoint << setprecision(2);
    outfile << "Employees Initials : " << Name << endl;
    outfile << "Total Hours Worked : " << TotalHoursWorked << endl;
    outfile << "Hourly Rate Pay : " << HourlyRate << endl;
    outfile << "Gross Pay : " << GrossPay << endl;
    outfile << "Federal Withholding : " << FedWithholding << endl;
    outfile << "State Withholding : " << StateWithholding << endl;
    outfile << "Union Dues : " << UnionDues << endl;
    outfile << "Hospitalization : " << Hospitalization << endl;
    outfile << "Total Deductions : " << Deductions << endl;
    outfile << "Net Pay : " << NetPay << endl;
}

Please use code tags: http://www.cplusplus.com/articles/z13hAqkS/

You have comma at the end of some of the lines, needs to be semicolon ;

This statement has no effect, and make no sense given that RateError is a bool:
RateError < 0.0;

Prefer double rather than float. float's precision is easily exceeded.

Try to avoid global variables.

Declare 1 variable per line, ideally wait until you have a sensible value to assign to it, do declaration and assignment in 1 statement. Always initialise your variables.

In the function declarations, give the parameters the same name as in the function definition.

Pass string by const reference:

1
2
3
4
void SendData(const string& Name, 
                const float TotalHoursWorked, 
                // .....
                )


Use const wherever you can.

Good Luck !!
All data will be input from a file Payln.Txt (See below).Develop a subprogram for each-task involved in this problem. Use value and reference parameters for passing all data between modules, Determine the total hours worked by each employee during the week. Calculate each employees total pay based on the following scale of hours worked:

0 through 40 hours regular pay
more than 40 through 60 hours 1 1/2 pay
more than 60 through 80 hours double pay

Determine deductions (state withholding, federal withholding, union dues, and hospitalization) for each employee (as calculated in Lab 2A). Determine the gross pay (before deductions) and net pay (after deductions).
Output to a new file--PayOut.Txt: 3 employee initials, total hours worked, pay rate, the amount of each deduction (federal withholding, state withholding, union dues, and hospitalization), gross pay, and net pay.
Input
3 employee initials, 7 numbers (representing the hours worked during 7 days), and the employee's hourly rate of pay. Error check data input from the file (by the input module). Create the data file below using your text editor or Notepad.
Data File
ABC 8.0 8.0 8.0 8.0 8.0 0.0 0.0 8.75
DEF 10.0 10.0 10.0 10.0 10.0 0.0 0.0 9.75
GHI 8.0 10.0 6.0 10.0 9.0 10.0 12.0 10.00
JKL 0.0 0.0 5.0 6.0 7.0 8.0 6.0 8.75
MNO 8.0 8.0 8.0 8.0 8.0 8.0 80.0 8.00
PQR 10.0 10.0 8.0 10.0 6.0 3.0 2.0 9.75
STU 9.0 11.5 5.5 7.5 9.5 -2.5 0.0 11.50
VWX 25.0 0.0 0.0 8.5 7.5 5.5 0.0 12.50
XYZ 10.0 10.0 10.0 10.0 10.0 0.0 0.0 -5.00
AAA 0.0 0.0 0.0 25.0 1.0 0.0 0.0 15.75

This is actually my problem I'm trying to solve based from the code, my programs runs but didn't input these record above on a file PayOut.txt, I think I need to include for loop on my main?

Hello instinct,

Once I fixed the errors that chicofeo mentioned and the errors went away the first thing I noticed is that you are reading the "PayIn.txt" file wrong. In ABC 8.0 8.0 8.0 8.0 8.0 0.0 0.0 8.75 what does each number refer to? In main you read infile >> Name >> HoursWorked >> HourlyRate then in "GetData" again you read infile >> Name >> HoursWorked >> HourlyRate, but the file pointer is not at the beginning of the next line, so when you read "Name" the second time it actually reads a number.

I still need to work through the program to figure out how it works. This is just what I found first. I would concentrate on reading the "PyIn.txt" file and storing the data first and the rest of the program should fall into place easier.

Hope that helps,

Andy
The ABC refers to initial of the employees, 7 numbers are the hours worked in 7days he worked and the last number is the regular rate. My program needs to read the 10 rows and needs to read seven times for the hours in order to calculate the total hours... I have to revise my code actually.
Here's the part of my code: No errors but it read 10X on each records, i can't figure out why it keeps doing that it suppose to read a record once:

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
while (!infile.eof())
	{
		GetData (Name, TotalHoursWorked, HourlyRate, HoursError, RateError);
		if (!HoursError && !RateError)
		{
			Calc(HoursWorked,TotalHoursWorked, HourlyRate, GrossPay, FedWithholding, StateWithholding, UnionDues, Hospitalization, TotalDeductions, NetPay);
			SendData(Name, TotalHoursWorked, HourlyRate, GrossPay, FedWithholding, StateWithholding, UnionDues, Hospitalization, TotalDeductions, NetPay);
		}  //end if
	}    // end while
	outfile << fixed << showpoint << setprecision(2);
	infile.close();
	outfile.close();
	errorfile.close();
	return 0;
}  // main end braces

   // read data from the files and checking errors
void GetData(string& Name, float& TotalHoursWorked, float& HourlyRate, bool HoursError, bool RateError)   //function definition for GETDATA
{
	int rows = 10, col = 8;
	int i, j; 
	float HoursWorked;
	float Hour1, Hour2, Hour3, Hour4, Hour5, Hour6,Hour7;
	infile >> Name >> Hour1 >> Hour2 >> Hour3 >> Hour4 >> Hour5 >> Hour6 >> Hour7 >> HourlyRate;
	for (i = 0; i < rows; i++)
	{
		outfile << "Employees Hours on records:" << endl;
		outfile << endl;
		for (j = 0; j < col; j++)
		{
			outfile << Name << " " << Hour1 << " " << Hour2 << " " << Hour3 << " " << Hour4 << " " << Hour5 << " " << Hour6 << " " << Hour7 << " " << HourlyRate << endl;
			HoursError = HoursError < 0 || HoursError > 24;
			RateError = RateError < 0;
			if (HoursError)
				errorfile << "Data error, should be at least 24 hours:" << Name << " " << Hour1 << " " << Hour2 << " " << Hour3 << " " << Hour4 << " " << Hour5 << " " << Hour6 << " " << Hour7 << " " << endl;
			if (RateError)
				errorfile << "Data error, regular rate is $8.75: " << Name << " " << HourlyRate << endl;
		}
		outfile << fixed << showpoint << setprecision(2);
		HoursWorked = Hour1 + Hour2 + Hour3 + Hour4 + Hour5 + Hour6 + Hour7;
		outfile << "Total Hours Worked: " << HoursWorked << endl;
	}
}[code]
[/code]
Last edited on
Hello instinct,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Starting at the top the while condition should no be on "eof". This will not work the way you think it will. by the time the while condition checks for "eof" yo will most likely have processed a bad read which usually prints the last good record read giving you two outputs of the last record. I will show a possible fix shortly.

Line 10 does not need to be there. It has now use here because you have used the same in the "GetData" function. It could be better used at line 7 or earlier and only needs to used once in the program.

In the "GetData" function line 20 int rows = 10, col = 8;, not the best idea because the size of the data file could change then you would have to find and change these variables for the program to work. Global variables would be easier to use, but still the wrong approach.

The use of "outfile" has no real use in the "Getdata" function because what you are doing here is done by the "SendData" function or should be. The "GetData" function should be only a read and process input function. An example the "TotalHoursWorked" is passed to the function, but never used. You can read the line of data and add all the hours for each day and add the accumulated data to "TotalHoursWorked".

The for loops starting at line 25 have nor real use here unless you want to completely change the look and information in the output file.

Lines 32 and 33 I do not believe that is working the way you are thinking. HoursError = HoursError < 0 || HoursError > 24; I do not understand what you are trying to do on the rhs of =. "HoursError" is defined as a bool so the only two possibilities for a bool are "0" or "1" "false" or "true". Technically "HourErros" can not be less than "0" or grater than "1" although sometimes any number greater than "1" is considered as true. The way that "HoursError" or "RateError" would be set to "false" or "true" would be by checking some variable to see if it is in the proper range. An example:
1
2
if (HourlyRate < 8.75)
    RateError = true;


Your if statements starting at line 34 may not work the way you want them to.

Line 39 for "setprecision" is in the wrong place. It should be before the first output of a decimal number or if you have used it in main, it is not needed here.
Line 50 "HoursWorked" should be "TotalHoursworked", so the value gets back to main and does not die when the function ends.

Having Tried to explain what I see as the problems. the code in your original post is good. The only problem was reading the file and setting up the data. This is what I came up with to make the program work:

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
    bool cont{ true };  // <--- somewhere bovee main as a gloal variable.
	//infile >> Name >> HoursWorked >> HourlyRate;  // <--- This should be done in getData.

	while (cont)
	{
		GetData(Name, HoursWorked, HourlyRate, HoursError, RateError);

		if (!cont) continue;  // <--- Bypasses everything when eof.

		Calc(HoursWorked, HourlyRate, TotalHoursWorked, GrossPay, FedW, StateW, Union, Hospital, TotalDeductions, NPay); //error undeclared identifier
		SendData(Name, TotalHoursWorked, HourlyRate, GrossPay, FedW, StateW, Union, Hospital, TotalDeductions, NPay); //error undeclared identifier

		HoursWorked = 0.0;  // <--- Resets all the variables before the next read.
		HourlyRate = 0.0;
		TotalHoursWorked = 0.0;
		TotalDeductions = 0.0;
		GrossPay = 0.0;
		FedW = 0.0;
		StateW = 0.0;
		NPay = 0.0;
		Union = 0.0;
		Hospital = 0.0;
	}

	void GetData(string &Name, double &HoursWorked, double &HourlyRate, bool HoursError, bool RateError)
{
	double hrs;

	infile >> Name;

	if (infile.fail())
	{
		cont = false;
		return;
	}

	for (size_t lp = 1; lp < 8; lp++)
	{
		infile >> hrs;
		HoursWorked += hrs;
	}

	infile >> HourlyRate;
}


With these changes the program worked and the output was:

Employees Initials : ABC
Total Hours Worked : 40.00
Hourly Rate Pay : 8.75
Gross Pay : 350.00
Federal Withholding : 63.00
State Withholding : 15.75
Union Dues : 7.00
Hospitalization : 25.65
Total Deductions : 111.40
Net Pay : 238.60

The "Calc" function I left alone although it could use some work. Having to do with "Hospitalization" and "UnionDues" if these values are not needed for a deduction. Just a thought.

For the "SendData" function I put a "\n" in the last line after "NetPay" to separate each name group and make it easier to read the file.

Hope that helps,

Andy

Edit: To your question "GetData" reads 10 times because of your for loops.
Last edited on
Hi Andy thank you so much for you help, still i have another problem to solve, I can't figure out my equation for the same problem above.. here's the condition:

All data will be input from a file Payln.Txt (See below).Develop a subprogram for each-task involved in this problem. Use value and reference parameters for passing all data between modules, Determine the total hours worked by each employee during the week. Calculate each employees total pay based on the following scale of hours worked:
0 through 40 hours regular pay
more than 40 through 60 hours 1 1/2 pay
more than 60 through 80 hours double pay

This is my code for Calculations part:

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
void CalcData(double& TotalHours, double& hourlyrate, double& TotalWage, double& FedTax, double& StateTax, double& UnionTax, double& TotalDeduction, double& NetPay)
{
	double BaseHours, BasePay, OvertimeHours, OvertimePay, ExtraHours, ExtraPay, OvertimeRate, ExtraRate;
	BasePay = 0;
	OvertimePay = 0;
	ExtraPay = 0;
	
	OvertimeHours = TotalHours - 40;
	BaseHours = TotalHours - OvertimeHours;

	if (TotalHours > 60 || TotalHours >= 80)
	{
		ExtraHours = 60;
		ExtraHours = ExtraHours - TotalHours;
	}
	else
	{
		ExtraHours = 0;
	}
		
	OvertimeRate = hourlyrate * 1.50;
	ExtraRate = hourlyrate * 2.00;
	BasePay = BaseHours * hourlyrate;
	OvertimePay = OvertimeHours * OvertimeRate;
	ExtraPay = ExtraHours * ExtraRate;

	TotalWage = BasePay + OvertimePay + ExtraPay;
	FedTax = TotalWage * FEDERAL;
	StateTax = TotalWage * STATE;
	UnionTax = TotalWage * Union;
	TotalDeduction = FedTax + StateTax + UnionTax + Hospital;
	NetPay = TotalWage - TotalDeduction;
}

Here's my Output:
Name: ABC
Hourly Rate : $8.75
Hours Worked: 40.00
Toltal Wages: $350.00

Federal Tax: $63.00
State Tax: $15.75
Hospitalization: $25.65
Union Tax: $7.00
Total Deductions: $111.40
NetPay: $238.60

Name: DEF
Hourly Rate : $9.75
Hours Worked: 50.00
Toltal Wages: $536.25

Federal Tax: $96.53
State Tax: $24.13
Hospitalization: $25.65
Union Tax: $10.72
Total Deductions: $157.03
NetPay: $379.22


Name: GHI
Hourly Rate : $10.00
Hours Worked: 65.00
Toltal Wages: $675.00

Federal Tax: $121.50
State Tax: $30.38
Hospitalization: $25.65
Union Tax: $13.50
Total Deductions: $191.03
NetPay: $483.97

Name: JKL
Hourly Rate : $8.75
Hours Worked: 32.00
Toltal Wages: $245.00

Federal Tax: $44.10
State Tax: $11.03
Hospitalization: $25.65
Union Tax: $4.90
Total Deductions: $85.68
NetPay: $159.32


Name: PQR
Hourly Rate : $9.75
Hours Worked: 49.00
Toltal Wages: $521.63

Federal Tax: $93.89
State Tax: $23.47
Hospitalization: $25.65
Union Tax: $10.43
Total Deductions: $153.45
NetPay: $368.18

The five data's was the only one processed since no errors on hourly rate and hours worked. I got the correct results on 3 data but for Hours worked below 40 hours and above 60 hours my results is wrong. I can;t figure out the correct equation or shall I need to create a separate function for that, please help thanks!
Last edited on
Hi, instinct,

you really don’t like using the “code” tag, do you?
People here are doing their best to help you, so try to imagine what you could achieve if you provided a minimum compilable code which reproduces your errors!
You’d be likely to get the solution in minutes, especially if it were included between “code” tags.

Happy coding!
Hello instinct,

Your new bit of code does not match what you have done previously. It will take me a little while to make it work.

The first thing I noticed is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (TotalHours > 60 || TotalHours >= 80)
{
ExtraHours = 60;
ExtraHours = ExtraHours - TotalHours;
}
else
{
ExtraHours = 0;
}

OvertimeRate = hourlyrate * 1.50;
ExtraRate = hourlyrate * 2.00;
BasePay = BaseHours * hourlyrate;
OvertimePay = OvertimeHours * OvertimeRate;
ExtraPay = ExtraHours * ExtraRate;


This does not follow
0 through 40 hours regular pay
more than 40 through 60 hours 1 1/2 pay
more than 60 through 80 hours double pay


"TotalHours" is not properly broken up into the three groups that are described. And the overtime rates of 1.5 and 2 are not used properly. When I fixed your original program, the output for the first two entries is:
Employees Initials : ABC
Total Hours Worked : 40.00
Hourly Rate Pay : 8.75
Gross Pay : 350.00
Federal Withholding : 63.00
State Withholding : 15.75
Union Dues : 7.00
Hospitalization : 25.65
Total Deductions : 111.40
Net Pay : 238.60

Employees Initials : DEF
Total Hours Worked : 50.00
Hourly Rate Pay : 9.75
Gross Pay : 405.00
Federal Withholding : 72.90
State Withholding : 18.22
Union Dues : 8.10
Hospitalization : 25.65
Total Deductions : 124.87
Net Pay : 280.13


You have a good start with this function. I would initialize the variables on line 8 before they are used. Also It is a good idea to keep your const defined variables the same as what you used in your original program. The same goes for the parameters received by the function. They need to be the same order as defined in main, the names can be different.

Hope that helps,

Andy
Thank you Andy for you help, I appreciate it a lot. I'm actually cramming and got stuck on this project... I need to run this program soon, everything works well in my functions.. except my output in one data is not correct.. the data is some above -- I don't know why my code is not reading on hours worked below 40 hours:
Here's my revised equation on the code:

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
void Calc(double& h_Worked, double& h_Rate, double& g_Pay, double& FedWithholding, double& StateWithholding, double& UnionDues, double& t_Deductions, double& n_Pay)
{
	double ot_Rate, d_Rate;
	double max_Hour = 0;
	double hours_Worked = 0;
	double reg_Pay, ot_Pay, d_Pay;
	ot_Rate = h_Rate * 1.50;
	d_Rate = h_Rate * 2.00;

	if (h_Worked < 40)
	{
		g_Pay = h_Worked * h_Rate;
	}
	else
	{
		max_Hour = 40;
		g_Pay = max_Hour * h_Rate;
	}
	if (h_Worked <= 60)
	{
		reg_Pay = (40 * h_Rate);
		ot_Pay = (h_Worked - 40)* ot_Rate;
		g_Pay = reg_Pay + ot_Pay;
	}
	else
	{
		hours_Worked = 60;
		reg_Pay = (40 * h_Rate);
		ot_Pay = (hours_Worked - 40) * ot_Rate;
		d_Pay = (h_Worked - 60) * d_Rate;
		g_Pay = reg_Pay + ot_Pay + d_Pay;
	}
	
	FedWithholding = g_Pay * FEDTAX_RATE;
	StateWithholding = g_Pay * STATETAX_RATE;
	UnionDues = g_Pay * UNION_DUES;
	t_Deductions = FedWithholding + StateWithholding + UnionDues + HOSPITALIZATION;
	n_Pay = g_Pay - t_Deductions;
	
}


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
Name: ABC
Hourly Rate : $8.75
Hours Worked: 40.00
Toltal Wages: $350.00

Federal Tax: $63.00
State Tax: $15.75
Hospitalization: $25.65
Union Tax: $7.00
Total Deductions: $111.40
NetPay: $238.60


Name: DEF
Hourly Rate : $9.75
Hours Worked: 50.00
Toltal Wages: $536.25

Federal Tax: $96.53
State Tax: $24.13
Hospitalization: $25.65
Union Tax: $10.72
Total Deductions: $157.03
NetPay: $379.22


Name: GHI
Hourly Rate : $10.00
Hours Worked: 65.00
Toltal Wages: $800.00

Federal Tax: $144.00
State Tax: $36.00
Hospitalization: $25.65
Union Tax: $16.00
Total Deductions: $221.65
NetPay: $578.35


Name: JKL
Hourly Rate : $8.75
Hours Worked: 32.00
Toltal Wages: $245.00      // MY Total Wages didn't calculate right , hours worked less then 40 
                                           didn't read at all 

Federal Tax: $44.10
State Tax: $11.03
Hospitalization: $25.65
Union Tax: $4.90
Total Deductions: $85.68
NetPay: $159.32

Name: PQR
Hourly Rate : $9.75
Hours Worked: 49.00
Toltal Wages: $521.63     

Federal Tax: $93.89
State Tax: $23.47
Hospitalization: $25.65
Union Tax: $10.43
Total Deductions: $153.45
NetPay: $368.18 


Please help! thanks so much!
Hi,

just a minor point.

I like this formatting if you have lots of function parameters:


1
2
3
4
5
6
7
8
9
void Calc(double& h_Worked, 
          double& h_Rate, 
          double& g_Pay, 
          double& FedWithholding, 
          double& StateWithholding, 
          double& UnionDues, 
          double& t_Deductions, 
          double& n_Pay )
{


IMO, it makes it easier to read: one can put const qualifiers and comments without the code being too many chars per line.

I am not so keen on variable names like h_Worked , I would rather have HoursWorked (if that is what it means). It is an aid to understanding if one doesn't have to guess what variables mean.

Also, rather than having magic numbers like 40 and 60 in the code, make them const variables and use the variable name instead.

constexpr double OrdinaryHours = 40.0;
Last edited on
Hello instinct,

I tend to agree with TheIdeasMan on his format idea for parameters, but more so on the variable names. The use of the "_" worked well in the old DOS environment, but these days the use of "_" is more reserved for header files and more for the header files that come with the IDE compiler package. Although there is nothing wrong with h_Worked in C++ it is more common to use the camel case format of hWorked or as I recently read using a more descriptive name like hoursWorked makes the code easier to understand. And six months from now when you go back to look at this program you can understand what the variables are and do not have to take time to figure out what variable names are for. Just my 2 cents.

Your last bit of code for the Calc function is better, but take a look at this and see how it differs. I believe it works with no problems. At least it has with the few I checked by hand. This code is based off your original code although I think I made changes to the function parameters to make it work correctly.

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
// Calculate total hours worked, grosspay, deductions and netpay
// changed per TheIdeasMan's suggestion
void CalcData(double& HoursWorked,  // <--- Changed to CalcData per your latest function revision.
	          double& HourlyRate,
	          double &TotalHoursWorked,
		  double &GrossPay,
		  double &FedWithholding,
		  double &StateWithholding,
		  double &UnionDues,
		  double & Hospitalization,
		  double &Deductions,
		  double &NetPay)
{
	double regularHours{ 0.0 }, overTimeHours{ 0.0 }, extraHours{ 0.0 };
	constexpr double overTimeRate{ 1.5 };
	constexpr double overTimeRate2{ 2.0 };

        // <--- The magic numbers need to be changed to constexpr variables.
	if (HoursWorked > 40)
	{
		if (HoursWorked > 40 || HoursWorked < 61)
		{
			overTimeHours = HoursWorked - 40;
			regularHours = HoursWorked - overTimeHours;
		}
		if (overTimeHours > 20)
		{
			extraHours = overTimeHours - 20;
			overTimeHours = overTimeHours - extraHours;
		}
	}
	else
		regularHours = HoursWorked;

	TotalHoursWorked =  HoursWorked;
	GrossPay = regularHours * HourlyRate;
	GrossPay += (overTimeHours * overTimeRate) + (extraHours * overTimeRate2);
	FedWithholding = FEDTAX_RATE * GrossPay;
	StateWithholding = STATETAX_RATE * GrossPay;
	UnionDues = UNION_DUES * GrossPay; //error undeclared identifier
	Hospitalization = HOSPITALIZATION; //error undeclared identifier
	Deductions = FedWithholding + StateWithholding + UnionDues + Hospitalization;
	NetPay = GrossPay - Deductions;
	//std::cout << std::endl;  // <--- Used for testing purposes.


See what you think.

Hope that helps,

Andy
Hi, instinct,

I’ve re-started your assignment from scratch. Let me know if it can be of any help.
Please note: I assumed hospitalization didn’t need any calculation, but just to be taken as a fixed amount.

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// All data will be input from a file Payln.Txt (See below).
// Develop a subprogram for each-task involved in this problem.
// Use value and reference parameters for passing all data between modules.
// Determine the total hours worked by each employee during the week.
// Calculate each employees total pay based on the following scale of
// hours worked:

// 0 through 40 hours --> regular pay
// more than 40 through 60 hours --> 1 1/2 pay
// more than 60 through 80 hours --> double pay

// Determine deductions
// (  - state withholding,
//    - federal withholding,
//    - union dues, and
//    - hospitalization)
// for each employee (as calculated in Lab 2A).
// Determine
//    - the gross pay (before deductions) and
//    - net pay (after deductions).

// Output to a new file--PayOut.Txt:
//    - 3 employee initials,
//    - total hours worked,
//    - pay rate,
//    - the amount of each deduction
//    - (  - federal withholding,
//         - state withholding,
//         - union dues, and
//         - hospitalization),
//    - gross pay, and
//    - net pay.

// Input:
//   - 3 employee initials,
//   - 7 numbers (representing the hours worked during 7 days), and
//   - the employee's hourly rate of pay.
// Error check data input from the file (by the input module).
// Create the data file below using your text editor or Notepad.
// Data File
// ABC   8.0   8.0   8.0   8.0   8.0   0.0   0.0   8.75
// DEF  10.0  10.0  10.0  10.0  10.0   0.0   0.0   9.75
// GHI   8.0  10.0   6.0  10.0   9.0  10.0  12.0  10.00
// JKL   0.0   0.0   5.0   6.0   7.0   8.0   6.0   8.75
// MNO   8.0   8.0   8.0   8.0   8.0   8.0  80.0   8.00
// PQR  10.0  10.0   8.0  10.0   6.0   3.0   2.0   9.75
// STU   9.0  11.5   5.5   7.5   9.5  -2.5   0.0  11.50
// VWX  25.0   0.0   0.0   8.5   7.5   5.5   0.0  12.50
// XYZ  10.0  10.0  10.0  10.0  10.0   0.0   0.0  -5.00
// AAA   0.0   0.0   0.0  25.0   1.0   0.0   0.0  15.75

#include <cmath>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

const std::string INPUT_FILE_NAME = "Payln.Txt";
const std::string OUPUT_FILE_NAME = "PayOut.Txt";

// deductions:
//    - state withholding
//    - federal withholding
//    - union dues
//    - hospitalization
struct Deduction {
   static constexpr double STATETAX_RATE    { 0.045};
   static constexpr double FEDTAX_RATE      { 0.18 };
   static constexpr double UNION_DUES       { 0.02 };
   static constexpr double HOSPITALIZATION  {25.65 };
};

struct EmplPayData {
   std::string name;
   double totalh    {};
   double pay_rate  {};
   double gross_pay {};
   double statetax  {};
   double fedtax    {};
   double uniondues {};
   double hospital  {};
   double net_pay   {};
};

std::vector<EmplPayData>& getPayDataFromFile(std::ifstream& infile,
                                            std::vector<EmplPayData>& empl);
std::vector<EmplPayData>& reckonEverything(std::vector<EmplPayData>& empl);
double calcGrossPay(EmplPayData& empl);
double calcStateTax(EmplPayData& empl);
double calcFedTax(EmplPayData& empl);
double calcUnionDues(EmplPayData& empl);
double calcHospitalization(EmplPayData& empl);
double calcNetPay(EmplPayData& empl);

void writeVectorOnFile(std::ofstream& outfile,
                       const std::vector<EmplPayData>& empl);

int main()
{
   std::vector<EmplPayData> empl;
   std::ifstream infile(INPUT_FILE_NAME);
   getPayDataFromFile(infile, empl);
   infile.close();

   reckonEverything(empl);
   std::ofstream outfile(OUPUT_FILE_NAME);
   writeVectorOnFile(outfile, empl);

   std::cout << "\nPress ENTER to continue\n";
   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

   return 0;
}

std::vector<EmplPayData>& getPayDataFromFile(std::ifstream& infile,
                                            std::vector<EmplPayData>& empl)
{
   EmplPayData tmpempl;
   while(infile >> tmpempl.name) {
      double temp;
      for(int i=0; i<7; i++) {
         if(infile >> temp) {
            if(temp < 0) { // can't accept negative number of hours
               temp = fabs(temp);
            } else if(temp > 24) { // max 24h a day
               temp /= 10; // suppose error in transcription
            }
            tmpempl.totalh += temp;
         } else {
            std::cout << "Can't elaborate input file\n";
            exit(EXIT_FAILURE);
         }
      }
      if(!(infile >> tmpempl.pay_rate)) {
         std::cout << "Can't elaborate input file\n";
         exit(EXIT_FAILURE);
      } else {
         if(0 > tmpempl.pay_rate) {
            tmpempl.pay_rate = fabs(tmpempl.pay_rate);
         }
      }
      empl.push_back(tmpempl);
   }

   return empl;
}

std::vector<EmplPayData>& reckonEverything(std::vector<EmplPayData>& empl)
{
   for(auto& e : empl) {
      calcGrossPay(e);
      calcStateTax(e);
      calcFedTax(e);
      calcUnionDues(e);
      calcHospitalization(e);
      calcNetPay(e);
   }

   return empl;
}

double calcGrossPay(EmplPayData& empl)
{
   // Calculate each employees total pay based on the following scale of
   // hours worked:
   // 0 through 40 hours --> regular pay
   // more than 40 through 60 hours --> 1 1/2 pay
   // more than 60 through 80 hours --> double pay
   double toth {empl.totalh};
   double gpay {};
   if(toth > 60) {
      gpay = (toth-60) * empl.pay_rate * 2;
      toth = 60;
   }
   if(toth > 40) {
      gpay += (toth-40) * empl.pay_rate * 1.5;
      toth = 40;
   }
   gpay += toth * empl.pay_rate;

   empl.gross_pay = gpay;

   return gpay;
}

double calcStateTax(EmplPayData& empl)
{
   empl.statetax = empl.gross_pay * Deduction::STATETAX_RATE;

   return empl.statetax;
}

double calcFedTax(EmplPayData& empl)
{
   empl.fedtax = empl.gross_pay * Deduction::FEDTAX_RATE;

   return empl.fedtax;
}

double calcUnionDues(EmplPayData& empl)
{
   empl.uniondues = empl.gross_pay * Deduction::UNION_DUES;

   return empl.uniondues;
}

double calcHospitalization(EmplPayData& empl)
{
   empl.hospital = Deduction::HOSPITALIZATION;

   return empl.hospital;
}

double calcNetPay(EmplPayData& empl)
{
   empl.net_pay = empl.gross_pay - empl.statetax - empl.fedtax - empl.uniondues
                  - empl.hospital;

   return empl.net_pay;
}

void writeVectorOnFile(std::ofstream& outfile,
                       const std::vector<EmplPayData>& empl)
{
   // Output to a new file--PayOut.Txt:
   //    - 3 employee initials,
   //    - total hours worked,
   //    - pay rate,
   //    - the amount of each deduction
   //    - (  - federal withholding,
   //         - state withholding,
   //         - union dues, and
   //         - hospitalization),
   //    - gross pay, and
   //    - net pay.
   for(const auto& e : empl) {
      outfile << e.name      << ' '
              << e.totalh    << ' '
              << e.pay_rate  << ' '
              << e.fedtax    << ' '
              << e.statetax  << ' '
              << e.uniondues << ' '
              << e.hospital  << ' '
              << e.gross_pay << ' '
              << e.net_pay   << '\n';
   }
}

Hi all, thanks for all your help, i am continually learning for all your input and suggestion.

For Andy, I trace your code and I see your equation works pretty well, so I made some changes on my Functions parameters. The initialization of TotalHoursWorked = HoursWorked i think is a good idea to be consistent on the value of hoursWorked to be calculated. Changed all my variables with underscore, and now my programs runs with correct result. I'm so happy thanks so much! :)

Hello instinct.

Your welcome, any time.

Andy
Topic archived. No new replies allowed.