My progam doesn't output some values to the selected file

I'm supposed to write a code that reads data from a file, "fines.dat", then output data which has fines exceeding 500 to a file, "Topfines.dat". The problem it only otuputs the the last fine. Here's my code:

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;
int main ()
{
string license, numberPlate;
double ticketFines;
double totalFines = 0.0;
ifstream infile;
infile.open("fines.dat");
if(infile.fail())
{
cout<<"File does not exist!";
return 0;
}
while(infile)
{
cout<<license<<setw(4)<<numberPlate<<setw(4)<<ticketFines<<endl;
if(ticketFines > 500){
ofstream infile;
infile.open("Topfines.dat");
infile<<license<<setw(4)<<numberPlate<<setw(4)<<ticketFines<<endl;
}
totalFines = totalFines + ticketFines;
getline (infile, license, '\t');
getline (infile, numberPlate, '\t');
infile>>ticketFines;
}
cout<<fixed<<showpoint;
cout<<setprecision(2)<<totalFines<<endl;
infile.close();
return 0;
}

You havent given the format of the data in the "fines.dat" file, but the code that appears to be missing is reading the data from infile.
You need to add this just before the while(infile) and also as the last lines in the while loop.
Topic archived. No new replies allowed.