Help with infile reading program

Hello guys, fairly new to programming and to this website in general. I need some help with a couple of sample programs, and I'll try to be as explicit as possible. Here is 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
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;

int main()
{
int idNumber, unionStatus;
double hours, rate, pay, dues, net, overtime, overtimePay;


ifstream inData;
ofstream outData;

inData.open("employee_data.txt");
outData.open("weekly_payroll.txt");

outData << fixed << showpoint;
outData << setprecision(2);
outData <<" Weekly Payroll " << endl;
outData << "ID No.        Hours         Rate       Pay          Member      Dues        NetPay " << endl;
outData << endl;


inData >> idNumber>> hours >> rate >> unionStatus >> idNumber>> hours >> rate >> unionStatus;

while (inData)
{
	while (idNumber != -1)
	{
		if (hours <= 40)
		{
			pay = hours* rate;
		}
	else if (hours> 40)
		{
			overtime = hours - 40;
			overtimePay = overtime * rate * 1.5;
			pay = hours* rate + overtimePay;
		}

	if (unionStatus == 1)
		{
			dues = .10 * pay;
			net = pay - dues;
		}
	else if (unionStatus == 0)
		{
			dues = pay - 5.00;
			net = pay - dues;
		}

outData << idNumber << setw(16) << hours << setw(13) << rate
<< setw(13)<< pay << setw(8)<< unionStatus << setw(16)<< dues<< setw(13) << net << endl;
}
break;
}

system("PAUSE");
return 0;
}


Here is the problem I am trying to solve:

Write a complete program including comments to compute a weekly payroll. The program reads in information for each of the employees of a company and then prints the results. The program does the following for each employee:

a. First, it reads in the data for each employee. Each employee has a three digit identification code (an integer from 100 to 999), hours worked, rate of pay, and union status (1 if the employee is in the union, 0 if not). For example, here are data valued for two typical workers:

123 46 6.50 1
456 32.12 3 0

b. The program computes the weekly pay, which comes from the following formula (this includes an overtime bonus of time and a half for each hour over 40):

if hours are less than or equal to 40, weekly pay is hours times rate
but
if hours are more than 40 hours, weekly pay is 40 times the rate plus the number of overtime hours times 1.5 times the rate.
Example: ID number 123 would get 40 * 6.50 + 6 * 1.5 * 6.50.

c. The program computes the union dues, which are 10 percent of the weekly pay for union members, and $5 for nonunion members.

d. The program computes net pay, which is weekly pay minus union dues.

e. The program prints all the data values read in and the words "Union" or "Nonunion", plus each item that has been computed. Make sure that all money amounts have exactly two decimal places.

Data: Make sure you handle at least 6 employees. Have at least two employees that are union members and two that are not. Make sure at least 3 employees get overtime.

Output: Create a good looking report. There should be a grand total at the bottom of the report for the total pay, union dues and net pay.


Read the data from an input file - Use the eof function.
Make sure that either a 1 or a 0 is entered for union status. If it is not a valid entry set your dues = 0.



My issue appears to be in my infile variable assignment. I don't know how to use a getline function, so I could use some advice on how to do that. I based this code off of a similar question someone asked here about 4 years ago. It runs, but just prints an infinite loop of data only from the first line of my input file.

If someone could help me to code it so it reads the input data line by line, I'm sure I could figure out how to code it to get the total data.

THANK YOU!!!
It runs, but just prints an infinite loop of data only from the first line of my input file.

That's because you only read from the infile once.

Delete line 27.
Change line 29 to:
 
while (inData >> idNumber>> hours >> rate >> unionStatus >> idNumber>> hours >> rate >> unionStatus)





I made your suggested change and it still prints the same thing
Did you remove the break statement at line 58? That will cause you to exit after reading the first record.
Yes. I don't think the issue is that the program is exiting after reading the first record, the problem is that it keeps reading the first record over and over and doesn't even get to the second or third line.

Other people are suggesting that I use a getline code, but I don't know how to do that, any tips?
http://www.cplusplus.com/forum/general/126889/

Very similar problem to this, trying to get the while function to read each individual line instead of repeating the first one, to avoid having to assign each item to a unique variable and making my code 6 times longer.

I KNOW there's a getline function, but I don't even know where to start, and the textbook I'm using doesn't offer any help. I need something to follow along with.
The problem is not so much the getline. That part is easy.
http://www.cplusplus.com/reference/string/string/getline/

The harder part is once you have read a line from the file into a string is how to parse it into the individual fields you want. The easiest way do to this is with a stringstream.
http://www.cplusplus.com/reference/sstream/stringstream/
The idea here is to create a stringstream from the record you read via getlne, then extract the individual fields from the stringstream.

1
2
3
4
5
string line;
while (getline (InData, line))
{  stringstream ss (line);
    ss >> idNumber>> hours >> rate >> unionStatus >> idNumber>> hours >> rate >> unionStatus;
}


That said, I think your problem is much easier and can be solved without using getline and stringstream. I'm guessing that you have data left in the InData buffer (e.g. \n) after executing line 29 I showed above. The way to solve that is to call InData.ignore to remove any remaining characters in the buffer before doing the next input operation.
http://www.cplusplus.com/reference/istream/istream/ignore/


Okay! I got it to run. I learned how to use a while (!infile.eos()) statement. The program is almost perfect, except now it's doubling the last line of data. Any ideas?

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

using namespace std;

int main()
{
int idNumber, unionStatus;
double hours, rate, pay, dues, net, overtime, overtimePay, totalPay, totalDues, totalNet;
string status;

ifstream inData;
ofstream outData;

inData.open("employee_data.txt");
outData.open("weekly_payroll.txt");

outData << fixed << showpoint;
outData << setprecision(2);
outData <<" Weekly Payroll " << endl;
outData << "ID No." << setw(11) << "Hours" << setw(14) << "Rate" << setw(14) << "Pay" 
	<< setw(14) << "Member" << setw(14) << "Dues" << setw(14) << "NetPay" << endl;
outData << "---------------------------------------------------------------------------------------- ";
outData << endl;

totalPay = 0;
totalDues = 0;
totalNet = 0;

while (!inData.eof())
{
	inData >> idNumber>> hours >> rate >> unionStatus;
		if (hours <= 40)
		{
			pay = hours* rate;
			totalPay = totalPay + pay;
				
			if (unionStatus == 1)
			{
				dues = .10 * pay;
				net = pay - dues;
				status = " Yes";
				totalDues = totalDues + dues;
				totalNet = totalNet + net; 
			}
			else if (unionStatus == 0)
			{
				dues = 5.00;
				net = pay - dues;
				status = "No";
				totalDues = totalDues + dues;
				totalNet = totalNet + net; 
			}
			else 
			{
				status = "Unknown";
				dues = 0;
				net = pay;
				totalDues = totalDues + dues;
				totalNet = totalNet + net; 
			}
				
		}

		else if (hours> 40)
		{
			overtime = hours - 40;
			overtimePay = overtime * rate * 1.5;
			pay = hours* rate + overtimePay;
			totalPay = totalPay + pay;
				
			if (unionStatus == 1)
			{
				dues = .10 * pay;
				net = pay - dues;
				status = " Yes";
				totalDues = totalDues + dues;
				totalNet = totalNet + net; 
			}
			else if (unionStatus == 0)
			{
				dues = 5.00;
				net = pay - dues;
				status = "No";
				totalDues = totalDues + dues;
				totalNet = totalNet + net; 
			}	
			else 
			{
				status = "Unknown";
				dues = 0;
				net = pay;
				totalDues = totalDues + dues;
				totalNet = totalNet + net; 
			}
		}

		

outData << idNumber << setw(14) << hours << setw(14) << rate
<< setw(14)<< pay << setw(14)<< status << setw(14)<< dues<< setw(14) << net << endl;
}

outData << endl << endl;
outData << "Totals" << setw(39) << totalPay << setw(28) << totalDues << setw(14) << totalNet;

system("PAUSE");
return 0;
}
The problem is line 32-34. After you read the last line of the file at line 34, eof is not yet set. You loop again, line 32 is true because eof is not set. You then attempt to read from inData at line 34. That fails and causes eof to now be set, but you don;t check for en error. You simply fall through the loop as if you had read a valid record.

Change lines 32-34 to:
 
  while (inData >> idNumber>> hours >> rate >> unionStatus)

That will both perform the input operations AND check that the result was good.
Topic archived. No new replies allowed.