Hi, I am trying to do challenge exercise 5.16 "Savings Account Balance" from the book "Starting out with C++".
I could do all but for the last part which is "If a negative balance is calculated at any point, a message should be displayed indicating the account has been closed and the loop should terminate."
Could someone please show me how to do that?
I'd appreciate it very much. Thank you!
Here is the question:
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:
A)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.
B)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.
C)Calculate the monthly interest. The monthly interest rate is the annual interest rate divided by twelve. 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.
NOTE:If a negative balance is calculated at any point, a message should be displayed indicating the account has been closed and the loop should terminate.
This is what I did.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
float interestRate;
double initialBalance, accountBalance,
monthlyDeposit, totalDeposit = 0,
monthlyWithdrawn, totalWithdrawn = 0,
monthlyInterestEarned, totalInterestEarned = 0;
int months, counter;
do /** ANNUAL INTEREST RATE **/
{
cout << "What is the annual interest rate? ";
cin >> interestRate;
if (interestRate < 0)
cout << "It should be a positive number.\n\n";
} while(interestRate < 0);
do /** STARTING BALANCE & add initial balance to account balance **/
{
cout << "What is the starting balance? ";
cin >> initialBalance;
if (initialBalance < 0)
cout << "It should be a positive number.\n\n";
} while(initialBalance < 0);
accountBalance = initialBalance;
do /** MONTHS PASSED **/
{
cout << "How many months have passed since the account was established? ";
cin >> months;
if (months < 0)
cout << "Months should be a positive number.\n\n";
} while(months < 0);
cout << endl;
/** to run each month **/
for (counter = 1; counter <= months; counter++)
{
cout << "Please enter the following info for month " << counter << ": " << endl;
do /** ask for monthly deposit, if wrong, repeat & add to accountBalance **/
{
cout << "How much money was deposited? ";
cin >> monthlyDeposit;
if (monthlyDeposit < 0)
cout << "It should be a positive number.\n\n";
} while (monthlyDeposit < 0);
accountBalance += monthlyDeposit;
do /** ask for monthly withdrawals, if wrong, repeat & SUB from accountBalance**/
{
cout << "How much money was withdrawn from account? ";
cin >> monthlyWithdrawn;
if (monthlyWithdrawn < 0)
cout << "It should be a positive number.\n\n";
} while (monthlyWithdrawn < 0);
accountBalance -= monthlyWithdrawn;
/** Calculate monthly interest earned & add to account balance **/
monthlyInterestEarned = accountBalance*(interestRate/12);
accountBalance += monthlyInterestEarned;
/** calculate for total amount of deposit, withdrawal & interest earned **/
totalDeposit += monthlyDeposit;
totalWithdrawn += monthlyWithdrawn;
totalInterestEarned += monthlyInterestEarned;
cout << endl;
}
/** Display result message **/
cout << fixed << setprecision (2);
cout << "Ending balance: " << accountBalance << endl
<< "Total deposit: " << totalDeposit << endl
<< "Total withdrawn: " << totalWithdrawn << endl
<< "Total interest earned: " << totalInterestEarned << endl;
return 0;
}
|