regarding

I have this code that works good, but I am wondering if I can use !input.eof() function in this case instead of while(input)..
The data in the text file is:
Ahmed 1500
Mohamed 600
Ali 3000

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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
	int money, sum=0, max=0;
	string name, nameswap;
	
	ifstream input;
	input.open("savings.txt");
	input>>name>>money;
	max=money;
	nameswap=name;
	cout<<"Customer name \t Saving money \n";
	while(input){
		cout<<name<<"\t"<<money<<endl;
		
		if(money>max)
		{
			max=money;
			nameswap=name;
		}
	sum+=money;
	
	input>>name>>money;

	}
	cout<<"Total saving amount= "<<sum<<endl;
	cout<<nameswap<<" has the highest saving "<<max;
	
	input.close();
	
	return 0;
}
What do you expect to gain? Actually, input can fail for other reasons than eof, so testing the stream for failure (as you are doing through istream's operator bool overload) is usual best. Although we often test the input function's return value itself:
1
2
while (input >> name >> money) {
}

Topic archived. No new replies allowed.