trying to write to a file

I'm trying to use a function to gather all inputted data and write that data to a file. I'm having trouble getting it to work.

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

using namespace std;

int totalExpenses(double[], double, string, int);
int totalIncome(double);
void saveExpense(int, double);


int main()
{

string month;
double otherExpense = 0;
double sum = 0;
string choice;
int total = 0;
const int Num_Bills = 5;
double income = 0;
double ratio =0;
string expenseFile;



ofstream outputFile;
outputFile.open("expense.txt");


cout << "What month are you entering? " << endl;
cin >> month;

string billNames[Num_Bills] = { "mortgage", "TimeWarner", "dpl", "creditCard", "cellPhone"};
double billVals[Num_Bills];

for (int i = 0; i < Num_Bills; i++)
{
cout << "Enter the dollar amount of your " << billNames[i] << endl;
cin >> billVals[i];
}

do
{
cout << "Do you have more expenses to enter? " << endl;
cin >> choice;


while (choice == "Yes" || choice == "yes" || choice == "y")
{
cout << "Enter the expense amount: " << endl;
cin >> otherExpense;
sum += otherExpense;
cout << "Do you have more expenses to enter? " << endl;
cin >> choice;

if (choice == "no" || choice == "No" || choice == "n")
{
cout << "The program will total your monthly expenses." << endl;

}

}


} while (choice == "no" || choice == "No");

total = totalExpenses(billVals, otherExpense, month, Num_Bills);
income = totalIncome(income);


if (income > total)
{
ratio = (total / income)*100;
cout << "Your expense ratio is " << ratio << " percent." << endl;

}
else
{
cout << "You have more expenses than income. You need to cut expenses." << endl;
}

/*outputFile << total << endl;
outputFile << income << endl;
outputFile << ratio << endl;
outputFile << totalExpenses << endl;

outputFile.close();*/

saveExpense(total, income);



system("pause");
return 0;


}

int totalExpenses(double billVals[], double otherExpense, string month, int size)
{
int total = 0;

for (int i = 0; i < size; i++)
{
total += billVals[i];

}
total += otherExpense;

cout << "Your total expenses for the month of " << month << " are: $" << total << endl;
return total;

}

int totalIncome(double income)
{
cout << "Enter your income for the month: ";
cin >> income;
return income;

}

void saveExpense(int total, double income)
{
ofstream outputFile;
outputFile.open("expense.txt");

total = totalExpenses(billVals, otherExpense, month, Num_Bills);
outputFile << total << endl;
outputFile << income << endl;



outputFile.close();
}
Near the end of the program in the saveExpense function, remove the line: "total = totalExpenses(...);" That function already recieves the total, see "int saveExpenses( int total, double income )."

Once you remove that line it should run no problem.

When casting a double to an int (i.e. total += otherExpenses) you should always use an explicit cast: "total += (int) otherExpenses;" That way you wont get a compiler warning. Alternately you could change the variable type of total to double; that actually makes more sense because money have dollars and cent (decimal) values. I wonder if the flood of double to int cast warnings are what made it hard for you to figure this one out.
Topic archived. No new replies allowed.