Question about a while loop, file input and output

Hi everybody. Now I know I shouldn't ask homework related questions, but I'm kind of stumped. I'm working on a program that reads data in from a file, 4 names and 4 balances, then through a menu it will either write the contents of the data file to a formatted file, or output the totals for the balances and average balance on the screen. I do have it working, but I have a question about using a while loop that has me stumped. The output to screen is working great. If I code the output to file to read in via infile>>data>>whatever, then follow it with outfile directly after, line by line, it outputs to the file just fine. If I do a block of infile, close the file, then a block of outfile and close the file, it only prints the last entry to the output file, as every entry. I did get this working so thats ok for now. My question is, I'm currently trying to do a while loop to read in from the file and have it output to the second file, but so far I've only managed to make it out put every entry as the last entry in the list that it reads in. I'm really new at this, but I'm wondering if what I'm trying to do can even be done with only what I've learned so far. Thanks ahead of time for any insight you can give me.

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;

int main()
{

//Declare Variables

int choice = 0, customers = 0;
string firstName, lastName;
double balance = 0.0, totalBalance = 0.0, averageBalance = 0.0;




//Intro
cout<<"Welcome to the Customer Balance Report Program!\n\n";

//Prompt user to select from the menu

do
{
	cout<<"Please Choose a Selection from the Menu.\n\n";
	//Display Menu
	//Select from the following menu
	//1) Create Customer Report
	//2) Display Total and Average Balance
	//3) Exit
	cout<<"1) Create Customer Report\n2) Display Total and Average Balance\n3) Exit\n\n";
	cin>>choice;
	cout<<endl;
	
		
		if (choice == 1)
			{
			//read in from file
			ifstream infile;
			ofstream outfile;
			
		 	//open file
		 	infile.open("customers.txt", ios::in);
		 	
		 	//validate file exists
		 		if(!infile)
		 		{
		 		cout<<"File does not exist!\n\n";
		 		}
		 		
		 	//read in data line by line
		 	while(infile>>firstName>>lastName>>balance)
		 	{
		 	
		 	outfile.open("report.txt", ios::out);
		 	
		 	outfile<<fixed<<showpoint<<setprecision(2);
		 	outfile<<"Name\t\tBalance Owed\n";
		 	outfile<<"-----------------------------\n";
		 	
		 	
		 	outfile<<firstName<<" "<<lastName<<"\t$"<<balance<<endl;
		 	
		 	outfile<<firstName<<" "<<lastName<<"\t$"<<balance<<endl;
		 	
		 	outfile<<firstName<<" "<<lastName<<"\t$"<<balance<<endl;
		 	
		 	outfile<<firstName<<" "<<lastName<<"\t$"<<balance<<endl;
		 	
		 	outfile.close();
		 	
		 	}
		 	//close file
		 	infile.close();
		 	//write formatted data to output file
		 	
		 
		 	//close output file
		 	
		 	}
		
		else if (choice == 2)
	
			{
			//read in from file
			
			ifstream infile;
			
		 	//open file
		 	
			infile.open("customers.txt", ios::in);
		 	
		 	
		 	//validate file exists
		 		if(!infile)
		 		{
		 		cout<<"File does not exist!\n\n";
		 		}
		 	
		 	//read in data line by line
		 	while(infile>>firstName>>lastName>>balance)
		 	{
		 	totalBalance += balance;
		 	customers++;
		 	}
		 	
		 
		 	//close file
		 	infile.close();
		 				 
		 	
		 	//calcuate totalBalance and averageBalance
		 	
	
		 	
		 	averageBalance = totalBalance / customers;
		 	
		 	//display totalBalance and averageBalance 
		 	cout<<fixed<<showpoint<<setprecision(2);
		 	cout<<"Total Balance Owed \tAverage Balance Owed\n";
		 	cout<<"-------------------------------------------\n";
		 	cout<<"$"<<totalBalance<<"\t\t\t$"<<averageBalance<<"\n\n";
		 	
		 	}
		 	
		else if (choice == 3)
			{
			//display goodbye
			cout<<"Thank you for using the Customer Balance Report Program.\n";
			}
		
		//Validate Input	
		else
			{
			//display invalid
			cout<<"Invalid Selection. You must choose from 1 through 3 on the menu.\n";
			}
			
	
}
while (choice != 3);	

return 0;
}
Last edited on
Every iteration of the loop, you open and close the output file.

1
2
3
4
5
6
7
8
9
10
while(infile>>firstName>>lastName>>balance)
{	 	
     outfile.open("report.txt", ios::out);
		 	
     // ...
		 	
     outfile.close();

     // ...
}


Without setting it to append, it will overwrite anything already in the file. If you set it to append, it will always append the output, giving you a long, redundant, file if you run the program more than once.
Ah OK I see what you're saying. Similar I guess to how when I did an infile<<firstName<<last name<<balance, and followed each one immediately with an outfile<<etc as the next line, it output all four in the correct order , ie
infile>>firstName>>last name>>balance
outfile<<firstName<<last name<<balance
infile>>firstName>>last name>>balance
outfile<<firstName<<last name<<balance

While this worked, it didn't look right to me.

Thanks I'll give it a try. Mainly trying to clean it up as much as possible. Thanks a ton.
Why not just read it all into a vector, or some other container. You only need to read it one time that way. Then you can easily change the list, print to screen, or write it back to file.
Topic archived. No new replies allowed.