Whenever I add the second expense type, it keeps adding the previous expenses as well so it runs looking like this;
MONTHLY EXPENSE PROGRAM
_______________________________________________________________________________________
This program will ask the type of the expense and the amount of expense for months.
The total expense will be calculated and the information will be written into a file
________________________________________________________________________________________
Enter the name of the file to write data to:
i
Enter the expense type:
u
Enter the amount of expense type for u for 4 months:
Amount of expense for month1:10
Amount of expense for month2:20
Amount of expense for month3:0
Amount of expense for month4:0
Total expense for u category is: 30
Do you want to enter another expense type <y/n>? y
Enter the expense type:
clothes
Enter the amount of expense type for clothes for 4 months:
Amount of expense for month1:0
Amount of expense for month2:10
Amount of expense for month3:0
Amount of expense for month4:0
Total expense for clothes category is: 40
Do you want to enter another expense type <y/n>? n
Your total expense in all categories is: 110
It's driving me crazy! Please help!
*****
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
//Display header
cout << "MONTHLY EXPENSE PROGRAM" << endl;
cout << "_______________________________________________________________________________________" << endl;
cout << "This program will ask the type of the expense and the amount of expense for months."<<endl;
cout << " The total expense will be calculated and the information will be written into a file" << endl;
//Display an error message while the input is invalid
while (expense <0) {
cout << "Invalid amount. Enter a non-negative value for month" << month<< ":" <<endl;
}
total += expense;
}
cout << "Total expense for " << expenseName << " category is: " << total << endl;
//Runs again if meets condition
cout << "Do you want to enter another expense type <y/n>? ";
cin >> choice;
cin.clear();
cin.ignore();
categoryTotal += total;
}
while (choice == 'y');
// Get the total amount spent for all categories
categoryTotal += total;
cout << "Your total expense in all categories is: " << categoryTotal;
cin >> categoryTotal;
outFile << "Your total expense for " <<expenseName << " is :"<< categoryTotal<<endl;
}
//Close the File
cout << "Your data has been saved to " << fileName << endl;
outFile.close();