fin1 xx.txt
--------------------
Paychecks 2500.00
Interest 100.00

fin2 xx.txt
--------------------
Car_Insurance 250.00
Cell_Phone 50.00
Book 200.00
Student_Loans 180.00
Rent 950.00

fin3 xx.txt
--------------------
Food 350.00

the output become
---------------------

Paychecks   2500.00      Car_Insurance  250.00      Food    350.00
Interest    100.00          Cell_Phone     50.00          Cell_Phone     50.00
                                   Book    200.00
                                   Student_Loans  180.00
                                   Rent           950.00

------------------------------
problem : occur 2 cell phone, why? how to solve this problem ?

while (  fin1.good() ||  fin2.good() ||  fin3.good())
					
	{	
		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);
		
		if ( !fin1.eof())    // i write 3 time of if ... which is same content and name fin1 fin2 and fin3
		{
			fin1 >> a >> b;
						
cout << setiosflags(ios::left) << setw(15)<< a << " RM "<< setw(7) << setprecision(2)<< b << " ";
			sum1 = b + sum1;
			count1 ++;
		}
		else
		cout << setiosflags(ios::left) << setw(27)<<"    "	;

Recommended Answers

All 3 Replies

I'd suggest using a single ifstream clearing and closing at the end of each file and reopening with the next file in sequence.

ifstream fin("x.txt")
while fin >> name
   fin >> amount
   cout << name  << ' ' << amount << ' '
   sum += amount
fin.clear()
fin.close()
fin.open("xx.txt")
while fin >> name
  fin >> amount
  cout << name  << ' ' << amount << ' '
  sum += amount
fin.clear()
fin.close()
fin.open("xxx.txt")
while fin>> name
   fin >> amount
   cout << name  << ' ' << amount << ' '
   sum += amount
fin.clear()
fin.close()
cout sum

When you do the fin3 >> a >> b; after having read all the data in that file, the inputs fail, but the values in the a and b variables remain unchanged. The eof( ) does not halt the reading until you've actually tried to read past the end.

Given the results you show, I suspect that files 1 and 2 don't end with a blank line, file 3 does have a final newline.

Use of eof( ) to test for end of file requires that you read an item outside the test/loop, if that's successful, read the remaining items(s) inside the test/loop. Better still:

if ( !fin1 >> a)   
   {
	fin1  >> b;
	//finish processing
   }

Val

Opss....i too careless....thx for remind me and the suggestion ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.