Write a program that calculates the balance of a saving account at the end of a
ID: 3824666 • Letter: W
Question
Write a program that calculates the balance of a saving account at the end of a three month period. It should ask the user for the starting balance. A loop then iterate once for every month in the period, performing the following steps: A. Ask the user for the total amount deposited into the account during the month and add it to the balance. Do not accept negative numbers. B. Ask the user for the total amount withdrawn form he account during the month and subtract it from the balance. Do not accept negative numbers.
After the last iteration, the program should call a function to that displays a report that includes the following information: • Starting balance • Total deposits made during the three months • Total withdrawals made during the three months • Final balance
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
// declaring variables
double annInterestRate = 0;
double startingBalance = 0;
int monthsPassed = 0;
double deposits = 0;
double withdrawal = 0;
double totDeposits = 0;
double totWithdrawal = 0;
double totInterest = 0;
double monthlyInterestRate = 0;
double totBalance = 0;
// obtain data from user
cout<<"Enter the annual interest rate on the account (e.g .04): ";
cin >> annInterestRate;
cout<<"Enter the starting balance: $";
cin >> startingBalance;
cout<<"How many months have passed since the account was established? ";
cin >> monthsPassed;
// initialize variables with the required info
totBalance = startingBalance;
monthlyInterestRate = annInterestRate/12;
// use a loop to obtain more data from user
for(int x=1; x <= monthsPassed; ++x)
{
cout<<"nMonth #"<<x<<endl;
cout<<"tTotal deposits for this month: $";
cin >> deposits;
totDeposits += deposits;
totBalance += deposits;
cout<<"tTotal withdrawal for this month: $";
cin >> withdrawal;
totWithdrawal += withdrawal;
totBalance -= withdrawal;
totInterest += (totBalance*monthlyInterestRate);
totBalance += (totBalance*monthlyInterestRate);
// check for negative values
// end loop if the values are negative
if(deposits < 0 || withdrawal < 0 || totBalance < 0)
{
cout<< "nPlease enter positive numbers only!";
cout<<"nThe account has been closed..n";
exit(1);
}
}
// display results
cout<<fixed<<setprecision(2);
cout.fill('.');
cout<<"nEnding balance:"<<left<<setw(20)<<right<<setw(20)<<" $ "<<totBalance;
cout<<"nAmount of deposits:"<<left<<setw(20)<<right<<setw(16)<<" $ "<<totDeposits;
cout<<"nAmount of withdrawals:"<<left<<setw(20)<<right<<setw(13)<<" $ "<<totWithdrawal;
cout<<"nAmount of interest earned:"<<left<<setw(20)<<right<<setw(9)<<" $ "<<totInterest<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.