193k views
2 votes
"Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. A loop should then iterate once for every month, performing the following: Ask the user for the amount deposited into the account during the month. (Do not accept negative numbers.) This amount should be added to the balance. Ask the user for the amount withdrawn from the account during the month. (Do not accept negative numbers.) This amount should be subtracted from the balance. Calculate the monthly interest. The monthly interest rate is the annual interest rate divided by 12. Multiply the monthly interest rate by the balance, and add the result to the balance. After the last iteration, the program should display the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned. The program should write the following data to an .txt file: ending Balance amount of deposits amount of withdrawal and the amount of interest earned Once the file has been written, display to the user that the 'Report written to Report.txt" c++

1 Answer

4 votes

Answer:

#include <iostream>

#include <fstream>

using namespace std;

int main() {

// Definitions

double annualInterest; // The annual interest rate

double balance; // The account balance

int months; // Total number of months

double totalDeposit = 0.0; // Total deposited value

double totalWithdraw = 0.0; // Total withdrawn value

double earnedInterest= 0.0; // The earned amount

double deposited; // Monthly deposited value

double withdrawn; // Monthly withdrawn value

// Get the initial required data.

cout<<"Please enter the annual interest rate,\\";

cout<<" Interest rate: ";

cin >>annualInterest;

cout<<"--------------------------------------------\\";

cout<<"Please enter the starting balance,\\";

do{

cout<<"[the number could not be negative]\\";

cout<<"Starting Balance: ";

cin >>balance;

}while(balance<0);

cout<<"--------------------------------------------\\";

cout<<"Please enter the number of months,\\";

do{

cout<<"[the number could not be less than 0]\\";

cout<<"Number of months: ";

cin >>months;

}while(months<0);

// Iterated Loop to get all the months data.

for(int month=1; month <= months; month++){

// Get the deposited value during that month.

cout<<"Enter the deposited value during month "

<<month<<",\\";

do{

cout<<"[the value could not be less than 0]\\";

cout<<" Deposited value: ";

cin >>deposited;

}while(deposited<0);

totalDeposit += deposited;

balance += deposited;

// Get the withdrawn value during that month.

cout<<"Enter the withdrawn value during month "

<<month<<",\\";

do{

cout<<"[the value could not be less than 0]\\";

cout<<" Withdrawn value: ";

cin >>withdrawn;

}while(withdrawn<0);

totalWithdraw += withdrawn;

balance -= withdrawn;

// Negative balance close the program.

if(balance < 0){

cout<<"Sorry, the account has been closed due to\\";

cout<<"the negative balance.\\";

break;

// Calculate value due to the interest rate.

}

else {

earnedInterest += (annualInterest/12) * balance;

// monthly interest rate

balance += (annualInterest/12) * balance;

}

}

// Display the statistics,

ofstream file;

file.open ("Report written to Report.txt");

file << "Report written to Report.txt\\";

file.close();

cout<<"The ending balance: "<<balance<<std::endl;

cout<<" Total deposited: "<< totalDeposit<<std::endl;

cout<<" Total withdrawals: "<< totalWithdraw<<std::endl;

cout<<" Earned interest: "<< earnedInterest<<std::endl;

cout<<"============================================\\";

return 0;

}

User Marco Rohner
by
5.9k points