While(!fin.eof()) infinite loop?

Hello everyone. First of all, sorry for another thread on such a common topic, but unfortunately I was not able to fix my program with the solutions found in other threads. I am a complete beginner, about halfway through an intro to programming course, so I know very little and am still getting the hang of many concepts.

I'm trying to write a program that reads data from a file, does some computations and prints the results to an output file. Here is the input file:


Willy Loman's Department Store
12-34
Bill Johnson
40.0 10.00
56-78
Ron McDonald
20.0 8.00
90-12
William Wilson
47.5 8.60
Lorilongname Longlastname
5.5 9.50


The code is supposed to use while(!fin.eof()) to keep reading until it's reached the end of the file. I'm finding that my code gets "stuck" each time it tries to run the loop. I'm not sure if it's in an infinite loop or what, but the dialog box will only print output done before the loop and then just has a blinking cursor afterwards. I tried running it in debug mode and every time I tried stepping over the loop it would just quit. I've also checked the output file, and it only prints output done before the loop as well.

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

using namespace std;

int main()
{
	// Date: 2/25/13
	// Programmer: cptEczema - CMPSC 121
	// Discussion: Produces an internal payroll report for a company

	// Named constants
	const double TAX=3.07;         // State tax rate
	const double CUTOFF=37.5;      // Overtime cutoff
	const double FACTOR=1.5;       // Overtime factor


	// Variables
	ifstream fin("program3.txt");  // Input file
	ofstream fout("program3.out"); // Output file
	string company;                // Company name
	string id;                     // Employee ID number
	string name;                   // Employee name
	double hours;                  // Total hours worked
	double rate;                   // Hourly rate
	double regHours;               // Regular hours worked
	double OThours;                // Overtime hours worked
	double grossPay;               // Employee's gross pay
	double regPay;                 // Employee's regular pay
	double OTpay;                  // Employee's OT pay
	double netPay;                 // Employee's pay after tax
	double taxedPay;               // Amount of employee's pay deducted due to the state tax
	double totalGrossPay;          // Total gross pay for all employees
	long counter;                  // # of employees


	// Open data file and read company name
	getline(fin, company);

	// Inform user that data is being processed
	cout<<"Company: "<<company<<endl<<endl;
	cout<<"Please wait while the data is processed...\n";
	
	// Print top part of the output to output file
	fout<<"ID    Name    Hours    Rate    Reg Hrs    OT Pay    Gross    State Tax    Net Pay"<<endl;
	fout<<"*********************************************************************************"<<endl;

	// Initialize the employee counter and the total gross pay
	counter=0;
	totalGrossPay=0;
	
	// Read the first employee ID number from file
	fin>>id;

	// Loop until no more data
	while(!fin.eof());
	{
		// Read the rest of the data for this employee
		getline(fin, name);
		fin>>hours;
		fin>>rate;
	
		// Compute the number of reg hours and overtime hours worked
		if(hours>CUTOFF)
		{
			regHours=CUTOFF;
			OThours=hours-regHours;
		}
		else
		{
			regHours=hours;
			OThours=0;
		}

		// Calculate the reg pay, OT pay, gross pay, tax, and net pay for employee
		regPay=regHours*rate;
		OTpay=OThours*rate*FACTOR;
		grossPay=regPay+OTpay;
		taxedPay=grossPay*TAX/100;
		netPay=grossPay-taxedPay;

		// Count this employee and update the total gross pay
		counter++;
		totalGrossPay+=grossPay;

		// Print the next line of the table
		fout<<id<<"    "<<name<<"    "<<hours<<"    "<<rate<<"    "<<regHours<<"    "<<OThours<<"    "<<regPay<<"    "<<OTpay<<"    "<<grossPay<<"    "<<TAX<<"    "<<netPay<<endl;

		// Read the next employee ID from the data file
		fin>>id;
	}
	fin.close();
	// Print the summary at the bottom of the output file and close the output file
	fout<<"Number of employees processed: "<<counter<<endl;
	fout<<"Overtime paid after:           "<<CUTOFF<<" hours\n";
	fout<<"Overtime factor:               "<<FACTOR<<endl;
	fout<<"State tax rate:                "<<TAX<<" %\n\n";
	fout<<"Total gross pay:               "<<totalGrossPay<<endl<<endl;
	fout<<"End-of-Report \n";
	fout.close();
	cout<<"Number of employees processed: "<<counter<<endl;
	cout<<"Results can be found in \"program3.out\".\n";

	return 0;
}


I've spent hours messing with things trying to get it to work, checking threads on this board and searching the internet, but I just don't know what is wrong with it. Any help would be greatly appreciated, thank you.


Last edited on
c++ eof function sometimes does not behave as expected. An alternate way of checking end of file is explained in this link http://mathbits.com/MathBits/CompSci/Files/End.htm
1
2
3
while (getline(fin,id)) { // loop works till getline has something to read
  ...
}
@abhishekm: do you mean "till getline has nothing to read"?
yes sorry chipp, bad english.. and thanks for correcting :)
I found several problems.

1. The data. There seems to be a missing employee id for the last person:
Lorilongname Longlastname
5.5 9.50

2. line 58:while(!fin.eof()); the semicolon should be removed.

3. Lastly, and perhaps more tricky. After reading the employee id using fin>>id; there will be a newline character remaining in the input buffer. This causes the next getline() to retrieve an empty line, and throws everything out of sync. To fix this, insert after the opening brace at line 59:
 
        fin.ignore(); // ignore newline character 



^Thank you, Chervil! Apparently my professor forgot that last id and I made a stupid mistake with that extra semi-colon. Your final point is what really fixed the issue for me though, after inserting that line of code everything works. Thanks again!
Topic archived. No new replies allowed.