Program idea: Console will prompt user to key in their fuel consumption details. After a full set of data is keyed in (date, amount, brand and mileage) and I continue on with the program, logging in as much as I want and finally decide to output the text into notepad, the text document will show me all the entries I've keyed in. The for loop works like it should, looping infinitely until user inputs 'N' or 'n'.
ISSUE: My issue with this is that no matter how many entries I key in and continue, my output text document will only show me the last entry I keyed in. Why is this so?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int count = 0;
double amount, mileage;
string date, brand;
char ans;
cout << "****************************" << endl;
cout << "CAR FUEL CONSUMPTION HISTORY" << endl;
cout << "****************************" << endl;
for (;;)
{
cout << "\nDate (dd/mm/yy): ";
cin >> date;
cout << "Amount Payable: $";
cin >> amount;
cout << "Brand: ";
cin >> brand;
cout << "Mileage (km): ";
cin >> mileage;
cout << "\nContinue? (Y/N): ";
cin >> ans;
if (ans == 'N' || ans == 'n')
{
break;
}
else
{
count++;
}
}
ofstream openFile;
openFile.open("conHist.txt");
// NOT WORKING!!!! >:(
while(openFile.good())
{
openFile << "Entry " << count << endl;;
openFile << "Date (dd/mm/yy): " << date << endl;
openFile << "Amount Payable: $" << amount << endl;
openFile << "Brand: " << brand << endl;
openFile << "Mileage: " << mileage << "km" << endl;
openFile.close();
}
return 0;
}