Output problems with data file and array. Need Help

Okay i edited my code, and now i cannot figure out why i cannot get my loop to procceed to next name in the data file. It is in and infinite loop and repeats the questions from the wkly_pay_roll function using the same person.

Here is the data file

Anderson Joe H 12.50 8000
Baker Sue H 10.25 6125.50
Thompson Randy S 32500 9525.00
Donner Mike H 13.10 9125.50
Conner Karen H 11.55 7125.00










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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <conio.h>

using namespace std;


void wkly_pay_roll(string, string, char, float, float, double&, double&, double&, double&);
float low_salary_deduction(float, float, double&, double&, double&, double&);
float high_salary_deduction(float, float, double&, double&, double&);
float low_hourly_deduction(float, float, double&, double&, double&, double&);
float high_hourly_deduction(float, float, double&, double&, double&);
int main()
{
	double state_tax = 0.03;
	double fed_tax = 0.08; 
	double ss_tax = 0.02;
	double net_pay = 0.00;
	ifstream infile;
	ofstream outfile;
	string fname[11], lname[11], name[11];
	char pay_type[11];
	float pay_rate[11];
	float year_to_date_gross[11];
	int i = 0;
	//char ans;

	//Open the data file and read in the information required.
	//Also checks to see if file is being opened right.
	infile.open("PAYMASTER.dat");
		
	if(!infile)
	{

		cout << "This is the inccorect file." << endl << endl;
		cout << "This is the location of the file: F:/Programming Foundations 1 CH 7/FINAL PROGRAM/FINAL PROGRAM." << endl;
		getchar();
		system ("cls");
	}
		for(i = 0; i < 5; i++)
		{
			infile >> lname[i] >> fname[i] >> pay_type[i] >> pay_rate[i] >> year_to_date_gross[i];
		}

	outfile.open("test.dat");

	for(i = 0; i < 5; i++)
	{
		outfile << setprecision(2) << showpoint << fixed;
		outfile << lname[i] << ',' << fname[i] << ' ' << pay_type[i] << ' ' << pay_rate[i] << ' ' << year_to_date_gross[i] << endl;
		wkly_pay_roll(lname[i], fname[i], pay_type[i], pay_rate[i],year_to_date_gross[i], state_tax, fed_tax, ss_tax, net_pay);
	}

	return 0;
}
void wkly_pay_roll(string lname, string fname, char pay_type, float pay_rate,  float year_to_date_gross, double& state_tax, double& fed_tax, double& ss_tax, double& net_pay)
{
	char ans;
	int hours_worked, ot_hours;
	
	for(int i = 0; i <= 5; i++)
	{
		if(pay_type == 'H')
		{
			cout << lname << ',' << fname << " is a hourly employee." << endl << endl;
			cout << "Please enter the hours worked for this employee." << endl << endl;
			cin >> hours_worked;
			year_to_date_gross = pay_rate * hours_worked;
			cout << "Did this employee work overtime? If yes type y or n." <<endl;
			cin >> ans;
				if(ans == 'y' || 'Y')
				{
					cout << "Please enter the amount of overtime hours the employee worked." << endl;
					cin >> ot_hours;
					ot_hours = ot_hours * 1.5;
					year_to_date_gross = year_to_date_gross + ot_hours;
						if(pay_rate < 30000)
						{
							low_hourly_deduction(pay_rate, year_to_date_gross, state_tax, fed_tax, ss_tax, net_pay);
						}
						else if(pay_rate > 30000)
						{	
							high_hourly_deduction(pay_rate, year_to_date_gross, state_tax, fed_tax, net_pay);
						}
				}		
		}
					else if(pay_type == 'S')
					{
						if(pay_rate < 30000)
							low_salary_deduction(pay_rate, year_to_date_gross, state_tax, fed_tax, ss_tax, net_pay);
						else if(pay_rate > 30000)
							high_salary_deduction(pay_rate, year_to_date_gross, state_tax, fed_tax, net_pay);
					
		}
	}
}



float low_salary_deduction(float pay_rate, float year_to_date_gross, double& state_tax, double& fed_tax, double& ss_tax, double& net_pay)
{
	state_tax = state_tax * pay_rate;
	fed_tax = fed_tax * pay_rate;
	ss_tax = ss_tax * pay_rate;
	return net_pay = year_to_date_gross - state_tax - fed_tax - ss_tax;
}
float high_salary_deduction(float pay_rate, float year_to_date_gross, double& state_tax, double& fed_tax, double& net_pay)
{
	state_tax = state_tax * pay_rate;
	fed_tax = fed_tax * pay_rate;
	return net_pay = year_to_date_gross - state_tax - fed_tax;
}
float low_hourly_deduction(float pay_rate, float year_to_date_gross, double& state_tax, double& fed_tax, double& ss_tax, double& net_pay)
{
	state_tax = state_tax * pay_rate;
	fed_tax = fed_tax * pay_rate;
	ss_tax = ss_tax * pay_rate;
	return net_pay = year_to_date_gross - state_tax - fed_tax - ss_tax;
}
float high_hourly_deduction(float pay_rate, float year_to_date_gross, double& state_tax, double& fed_tax, double& net_pay)
{
	state_tax = state_tax * pay_rate;
	fed_tax = fed_tax * pay_rate;
	return net_pay = year_to_date_gross - state_tax - fed_tax;
}
Last edited on
Why would you need to pass index of array, if the functions themselves are not aware of any arrays? The problem is probably something else:
In line 55, isn't the function wkly_pay_roll called outside the for loop, meaning it is only done once, for the employee at record [5]. Do you even have 6 employees in the input file?

Also, I am pretty sure you are mishandling employees with payrate exaclty 30000
Last edited on
Yea i fixed that part. Now im stuck again with the loop not going on to the next employee in the data file.
You do not need the loop inside wkly_pay_roll(). Calling it 5 times in line 53 is enough.
Topic archived. No new replies allowed.