Please Help, The program keeps adding up previous expense type


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;

cout << "________________________________________________________________________________________" << endl;



// Inputs

char choice;
string fileName, expenseName;

//Open Output
ofstream outFile;
cout << "Enter the name of the file to write data to: " << endl;
cin >> fileName;
outFile.open(fileName);

//Unsuccessful Outfile
if (!outFile) {
cout << "Error opening outfile." << endl;

}

//If the file successfully opens, process it
else {

int month=1;
double total = 0.0, categoryTotal = 0.0, expense;

//Get the category

do {


cout << "Enter the expense type: " << endl;
cin >> expenseName;
cout << "Enter the amount of expense type for " << expenseName << " for 4 months: " << endl;
outFile << "Expense Type: " << expenseName;

//Get the total for each month


for (month = 1; month <= 4; month++) {


cout << "Amount of expense for month" << month << ":";
cin >> expense;


outFile << " month " << month << ": " << expense << " " << " month" << month << ": " << expense << " " << " month" << month << ": " << expense << " " << "month" << month << ": " << expense << "Total expense of this category is ======>" << total << 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();

return 0;
}


Last edited on
I believe your total variable is keeping the value between categories. Try having total = 0; right inside at the top of your do-while loop.
Last edited on
^ Thanks man! That was the exact problem!
Topic archived. No new replies allowed.