Hi! I need help with saving data into a text file. What I did won't work and I'm not sure what I'm doing wrong. I just need the results to save into a separate file that I have created in the project folder, after all of the calculations.
Also the loop must break if the balance is zero in any iteration and I don't think my program does that..
Can someone please help me with these two problems?
#include <iostream>
#include <iomanip>
#include <fstream>
usingnamespace std;
int main()
{
double annualInterest, startingBal,
accountBalance, withdrawn,
deposit;
double totalDeposit = 0, totalInterestEarned = 0,
monthlyInterestEarned, totalWithdrawn = 0;
int months, i;
ofstream outFile;
outFile.open("accountresults");
// Prompting the user to enter information
cout << "Enter the annual interest rate: " << endl;
cin >> annualInterest;
cout << "Enter the starting balance: " << endl;
cin >> startingBal;
accountBalance = startingBal;
cout << "Enter the number of months that have passed\n";
cout << "since the account was established: " << endl;
cin >> months;
// Running each month and gathering data
for (i = 1; i <= months; i++)
{
cout << "Enter the following information for month " << i << ": " << endl;
do // Asking for the monthly deposit
{
cout << "How much money was deposited? ";
cin >> deposit;
if (deposit < 0)
cout << "It should be a positive number.\n\n";
} while (deposit < 0);
accountBalance += deposit;
do // Asking for the monthly withdrawals
{
cout << "How much money was withdrawn from account? ";
cin >> withdrawn;
if (withdrawn < 0)
cout << "It should be a positive number.\n\n";
} while (withdrawn < 0);
accountBalance -= withdrawn;
monthlyInterestEarned = accountBalance * (annualInterest / 12);
accountBalance += monthlyInterestEarned;
totalDeposit += deposit;
totalWithdrawn += withdrawn;
totalInterestEarned += monthlyInterestEarned;
}
// Displaying the results
outFile << fixed << setprecision (2);
outFile << "Ending balance: " << accountBalance << endl;
outFile << "Total deposit: " << totalDeposit << endl;
outFile << "Total withdrawn: " << totalWithdrawn << endl;
outFile << "Total interest earned: " << totalInterestEarned << endl;
outFile.close();
return 0;
}
//If the balance is less than zero in any iteration, you must break your loop and send the information to the file along with a message.