Using a while loop with the eof() function

I have to use a while loop and the eof() function to produce the following output:
First Name: Don
Last Name: Smith
Department: Accounting
Title: Auditor
Monthly Salary: $4500.00
Bonus: 5%
Tax: 30%
Paycheck: $3307.50
I'm struggling with it. 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
 #include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	string firstName, lastName, department, title;
	float monthsal, percbonus, taxperc;
	
	ifstream inFile;

	inFile.open("inData.txt");

	while (!inFile.eof())
	{
		inFile >> firstName >> lastName >> department >> monthsal >> percbonus >> taxperc;
		float paycheck = ((monthsal * percbonus) + monthsal) - (taxperc * monthsal);
		cout << "First Name: " << firstName << endl << "Last Name: " << lastName << endl <<
			"Department: " << department << "Monthly Salary: " << monthsal << "Bonus: " << percbonus <<
			"Tax: " << taxperc << "Paycheck: $" << paycheck << endl;
	}

	inFile.close();

	return 0;
}

Any pointers? This is my first time using fstream data
What's the issue?

EDIT:

The only thing I can see that can be improved is the loop a tad:

1
2
3
4
5
6
7
while (inFile >> firstName >> lastName >> department >> monthsal >> percbonus >> taxperc)
{
		float paycheck = ((monthsal * percbonus) + monthsal) - (taxperc * monthsal);
		cout << "First Name: " << firstName << endl << "Last Name: " << lastName << endl <<
			"Department: " << department << "Monthly Salary: " << monthsal << "Bonus: " << percbonus <<
			"Tax: " << taxperc << "Paycheck: $" << paycheck << endl;
}
Last edited on
The problem is that eof() doen't work like people expect, but since this a requirement....
The issue is that EOF is set only when an extraction is attempted and doesn't succeed - not if the EOF is reached after a successful read. So inFile >> will cause an EOF after .eof() is false. - and data will not be extracted zapshe code is the way to do this - but doesn't use .eof(). If you really must use .eof() in a while loop, then:

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

using namespace std;

int main()
{
	string firstName, lastName, department, title;
	float monthsal, percbonus, taxperc;
	
	ifstream inFile;

	inFile.open("inData.txt");

	while (!inFile.eof())
	{
		if (inFile >> firstName >> lastName >> department >> monthsal >> percbonus >> taxperc) {
		    float paycheck = ((monthsal * percbonus) + monthsal) - (taxperc * monthsal);
    		    cout << "First Name: " << firstName << endl << "Last Name: " << lastName << endl <<
	    		"Department: " << department << "Monthly Salary: " << monthsal << "Bonus: " << percbonus <<
			"Tax: " << taxperc << "Paycheck: $" << paycheck << endl;
                }
	}

	inFile.close();

	return 0;
}

Hello kmcfall,

while (!inFile.eof())
This can work, but it seems as though no one really teaches the proper way to use it.

Given a file:

name 1
name 2
name 3



Your code:
1
2
3
4
5
6
7
8
9
10
while (!inFile.eof())
{
    inFile >> firstName >> lastName >> department >> monthsal >> percbonus >> taxperc;

    float paycheck = ((monthsal * percbonus) + monthsal) - (taxperc * monthsal);

    cout << "First Name: " << firstName << endl << "Last Name: " << lastName << endl <<
        "Department: " << department << "Monthly Salary: " << monthsal << "Bonus: " << percbonus <<
        "Tax: " << taxperc << "Paycheck: $" << paycheck << endl;
}

Works like this:
When you reach the while loop it first checks the condition. It evaluates to true because "eof" has not been set yet, so your next line reads "name 1" and processes the code returning to the while condition where it is still considered true and you enter the loop.

On this pass you read "name 2" and process it returning to the while condition where it is still considered true.

Next pass you enter the loop and read "name 3" and process it returning to the while condition.

Even though you have read the last name the condition is still considered true, so you enter the loop. This time there is nothing left to read, so the "eof" bit is set, but you are inside the while loop so you continue and either process what was left in the variable(s) or the variable(s) are set to zero or empty for strings and your output is incorrect.

Now that the "eof" bit is set the while condition will evaluate to "false" and the loop will end.

To properly use what you need try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
inFile >> firstName >> lastName >> department >> monthsal >> percbonus >> taxperc;

while (!inFile.eof())
{
    float paycheck = ((monthsal * percbonus) + monthsal) - (taxperc * monthsal);

    cout << "First Name: " << firstName << endl << "Last Name: " << lastName << endl <<
        "Department: " << department << "Monthly Salary: " << monthsal << "Bonus: " << percbonus <<
        "Tax: " << taxperc << "Paycheck: $" << paycheck << endl;

    inFile >> firstName >> lastName >> department >> monthsal >> percbonus >> taxperc;

}

This way when you check the while condition if the "eof" bit was not set you enter the while loop.

Doing the next read at the end if "eof" is set the while condition will fail at the proper time.

As a not the more accepted way to read a file of unknown size is:
while (inFile >> firstName >> lastName >> department >> monthsal >> percbonus >> taxperc). This way when the "eof" bit is set or any of the others the loop will fail.

Andy
To properly use what you need try this:


or have the extraction once in the while loop with a condition test as per my previous post.
Topic archived. No new replies allowed.