Apr 19, 2017 at 10:01pm UTC
Need help running the program again if the user selects to input a new expense. Also, nothing is copying over to the output file.
#include <iostream>
#include <string>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
string title = "MONTHLY EXPENSE PROGRAM"; //Title
cout << title << endl;
cout << "--------------------" << endl;
// Program description
cout << "This program will ask the type of expenses and amount of expenses for a total of 4 months.\n";
cout << "The total expense will be calculated, and the information will be written into a file.\n";
cout << "------------------" << endl;
{
ofstream outputFile;
string fileName; // Name of data file
string expenseName; // Name of category
float total; //Total of expenses for select category
float grandTotalExpenses; // Total of all expenses for all categories
char choice; // User option to add additional category
{
grandTotalExpenses=0;
while (expenseName !="Q")
total=0;
{
cout << "Enter the name of the file to write data to: " << endl;
cin >> fileName;
cout << "Enter the expense type: " << endl;
cin >> expenseName;
{
//Opening file
outputFile.open(fileName);
{
// Data
double amount1, amount2, amount3, amount4; // Positive amount of expense per month
double totalExpense;
// Expenses
cout << "Enter the amount spent for the " << expenseName << " expense for 4 months.... " << endl;
// Month1
cout << "Amount of expense for month 1: " << endl;
do
cin >> amount1;
while (amount1 <0);
{
cout << "Invalid entry! Please enter a positive number for month 1. " << endl;
cin >> amount1;
}
{
// Month2
cout << "Amount of expense for month 2: " << endl;
do
cin >> amount2;
while (amount2 <0);
{
cout << "Invalid entry! Please enter a positive number for month 2. " << endl;
cin >> amount2;
}
{
//Month 3
cout << "Amount of expense for month 3: " << endl;
do
cin >> amount3;
while (amount3 <0);
{
cout << "Invalid entry! Please enter a positive number for month 3. " << endl;
cin >> amount3;
}
{
// Month 4
cout << "Amount of expense for month 4: " << endl;
do
cin >> amount4;
while (amount4 <0);
{
cout << "Invalid entry! Please enter a positive number for month 4. " << endl;
cin >> amount4;
}
{
// Total Expenses
totalExpense = amount1 + amount2 + amount3 + amount4;
cout << setprecision(2) << fixed;
cout << "The total expense for 4 months for the category " << expenseName << " is: $" << totalExpense << endl;
cout << "\n";
// Additional catagories
do
{
cout << "Do you want to enter another expense category? (y/n) .\n" << endl;
cin >>choice;
}
while(choice=='y');
}
{
// Grand total of expenses for ALL categories
grandTotalExpenses+=totalExpense;
cout << "Your total expenses for all categories is: $" << grandTotalExpenses << endl;
//Writing expense to file
outputFile << fileName << endl;
// Closing file
outputFile.close();
cout << "Your expenses have now been saved to the file '" << fileName << ".txt'" << endl;
system( "read -n 1 -s -p \"...\"" );
return 0;
}
}