fileloop only outputs first line


Hello there. First time here.

My test Data:

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


The first problem I'm having is when I run the program, it only outputs the first line of test data to Outfile
and doesn't output the rest. It still reads the rest since the line for XYZ gets sent to the errorfile

My second problem is in my GetData function: doesn't read the negative -2.5 in STU as an error to the errorfile.
(note: This is for my class and we can't use arrays)



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
 #include <iostream>
# include <fstream>
# include <iomanip>
#include <string>
using namespace std;

void GetData(string &, double &, double &, double &, double &, double &, double &, double &, double &, double &, bool &, bool &, ifstream & Infile, ofstream & Errorfile);
void calcPay(double, double, double &, double &, double &, double &, double &, double &);
void Output(string, double, double, double, double, double, double, double, double, ofstream & Outfile);
int main()
{

	ifstream Infile;
	ofstream Outfile, Errorfile;
	string employee;
	double mon, tues, wed, thurs, fri, sat, sun, ratepay, total, grosspay, netpay, fedwithold, statewithold, unionDue, hospital;
	;
	bool workhourError, rateError;


	Infile.open("E:\\PayIn.txt");
	if (!Infile.is_open()) {
		cout << "Could not PayIn file." << endl;
		exit(1);
	}

	Errorfile.open("E:\\Errorfile.txt");
	if (!Errorfile.is_open()) {
		cout << "Error file could not be open" << endl;
		exit(1);
	}

	while (!Infile.eof())
	{
		GetData(employee, mon, tues, wed, thurs, fri, sat, sun, ratepay, total, workhourError, rateError, Infile, Errorfile);

		if (workhourError && rateError)
		{
			calcPay(total, ratepay, netpay, grosspay, fedwithold, statewithold, unionDue, hospital);
			Output(employee, total, ratepay, netpay, grosspay, fedwithold, statewithold, unionDue, hospital, Outfile);
		}
	}


	Infile.close();
	Outfile.close();




	return 0;
}
void GetData(string & employee, double & mon, double & tues, double & wed, double & thurs, double & fri, double & sat, double & sun, double & ratepay, double & total, bool & workhourError, bool & rateError, ifstream & Infile, ofstream & Errorfile)
{
	
	Infile >> employee >> mon >> tues >> wed >> thurs >> fri >> sat >> sun >> ratepay;
	if (mon && tues && wed && thurs && fri && sat &&  sun < 0)
	{
		workhourError = false;
	}
	else
		workhourError = true;
	
	if (ratepay < 0)
	{
		rateError = false;
	}
	else
		rateError = true;
	
	if (!rateError || !workhourError)
	{
		Errorfile << fixed << showpoint << setprecision(2);
		Errorfile << "Error in following data:" << employee << " " << mon << " " << tues << " " << wed << " " << thurs << " " << fri << " " << sat << " " << sun << ratepay << endl;
	}
	total = mon + tues + wed + thurs + fri + sat + sun;
}

void calcPay(double total, double ratepay, double & netpay, double & grosspay, double & fedwithold, double & statewithold, double & unionDue, double & hospital)
{
	cout << fixed << showpoint << setprecision(2);
	 if (total > 40 && total <= 60)
	{
		ratepay = (ratepay * 1.50);
	}

	else if (total > 60 && total <= 80)
	{
		ratepay = (ratepay *2.0);
	}
	grosspay = ratepay * total;
	fedwithold = grosspay * 0.18;
	statewithold = grosspay * 0.045;
	hospital = 25.65;
	unionDue = grosspay * .02;
	netpay = grosspay - fedwithold - statewithold - hospital - unionDue;

}

void Output(string employee, double total, double ratepay, double netpay, double grosspay, double fedwithold, double statewithold, double unionDue, double hospital, ofstream & Outfile)
{
	Outfile.open("E:\\PayOut.txt");
	if (!Outfile.is_open()) {
		cout << "PayOut file could not be open" << endl;
		exit(1);
	}

	Outfile << fixed << showpoint << setprecision(2);
	Outfile << "Employee:\t\t" << employee << "\nHourly Rate:\t\t  " << ratepay << "\nHours Worked:\t\t " << total << "\nGross Pay:\t\t" <<  grosspay << "\n Federal Withholding: \t " << fedwithold << "\nState Withholding: \t " << statewithold << "\nUnion Dues: \t\t  " << unionDue <<
	"\nHospitalization:\t" << hospital	<< "\nNet Pay:\t\t" << netpay << endl;

}


Any help is greatly appreciated and I apologize If I'm not clear with the way I explained.
> if (mon && tues && wed && thurs && fri && sat && sun < 0)
This does not make sure all your inputs are less than zero.

Maybe
if (mon > 0 && tues > 0 && wed > 0 && thurs > 0 && fri > 0 && sat > 0 && sun > 0)

oh! that fixed that issue. Thank you very much for your assistance. I also was able to figure out my loop issue. I kept opening the outfile in one of my functions.
Topic archived. No new replies allowed.