FileReader() -- Error -- Printing last line twice

Hello guys, i need help with this issue, i am trying to read a text file and process it, so i did it, but it read the last line twice and i would like your help to fix it. 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
void output_label2()
{

	ifstream fileReader;
	
	fileReader.open("Mailings.txt");
	

		
 	while(getline(fileReader, name))
	{  	
		
		getline(fileReader, address);
		getline(fileReader, city);                 
		getline(fileReader, state); 
		fileReader >> zipcode; 
		fileReader >> mailing; 
		fileReader >> weight;
		fileReader.ignore();
		fileReader.ignore();
		Barcode(); //function
		output_label_singleMailing(); //function
		
	

	}
	
	fileReader.close();

	total += postage;
	cout << " " << endl;
 	cout << " " << endl;
	cout << "Total cost of all the postage is $"<< total << endl;
	
	
}


*****Beginning of output********


***********************$0.71
Andy Dwyer
789 Cherry Road
Wamapoke IN 48034
|:|::||::|:||:::::||::|::|:::|||


***********************$0.49
Ben Wyatt
1513 Plymouth Street
Pawnee IN 47407
|:|::||:::|:|::|||:::|:::||::|:|


***********************$2.54
Chris Traeger
8900 Rapport Street
Eagleton IN 47322
|:|::||:::|::||:::|:|::|:|::|:||


Total cost of all the postage is $8.10


***********************$2.54
Chris Traeger
8900 Rapport Street
Eagleton IN 47322
|:|::||:::|::||:::|:|::|:|::|:||


*****End of output ********

you guys can notice that it outputs the info for "Chris Traeger" twice, i would love to eliminate the last one so it should display "Total cost of all the postage is $8.10" only at the end and not that second "Chris Traeger" information.

thank you so much.
Last edited on
cout << "Total cost of all the postage is $"<< total << endl; is the last thing in this function, and there is output after that. Whatever is causing that output can't be part of this function.

I see that you're using global variables (bad), so the data needed to output the address again does still exist after this function. If I had to guess, you're calling
output_label_singleMailing();
again somewhere.
Thank you so much, i went over my code and i found it, i had
output_label_singleMailing();
function within this function and an other one!!
and again how should i set up my global variables or should i use global variable? as i have 8 functions in my code.
should i use global variable?

No, learn to pass variables to and from your functions as required.


Topic archived. No new replies allowed.