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.
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
usingnamespace 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
}
elseif (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";
}
elseif (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;
}
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.