fork download
  1. //Alec White CS1A Chapter 5, P. 296, #16//
  2. /*******************************************************************************
  3.  *
  4.  * CALCULATE SAVINGS BALANCE
  5.  * _____________________________________________________________________________
  6.  *
  7.  * This program takes a starting balance of a savings account and the
  8.  * annual interest rate, and calculates the balance of the account per month
  9.  * after requesting the amount deposited/withdrawn per month and the accrual
  10.  * of interest. It then displays the results to the user after the given number
  11.  * of months has been calculated.
  12.  * _____________________________________________________________________________
  13.  * INPUT
  14.  * Annual Interest Rate : Annual interest rate offered by the bank
  15.  * Months Open : Months account has been open
  16.  * Starting Balance : Starting balance of the account
  17.  * Amount Deposited : Amount of money deposited from account per month
  18.  * Amount Withdrawn : Amount of money withdrawn from account per month
  19.  *
  20.  * OUTPUT
  21.  * Ending Balace : Final balance of the account after months open
  22.  * Total $ Deposited : Total number of $ deposited into the acct after months
  23.  * open.
  24.  * Total $ Withdrawn : Total number of $ withdrawn from the account after
  25.  * months open.
  26.  ******************************************************************************/
  27. #include <iostream>
  28. using namespace std;
  29.  
  30. int main() {
  31. float annualInterest;
  32. float monthlyInterest;
  33. float monthlyInterestAmount;
  34. float startingBalance;
  35. float amountWithdrawn;
  36. float monthlyDeposit = 0;
  37. float monthlyWithdrawn = 0;
  38. float currentBalance = 0;
  39. float totalWithdrawn = 0;
  40. float totalDeposited = 0;
  41. float totalInterestEarned = 0;
  42. int monthsOpen;
  43.  
  44. //prompt user for starting balance, annual interest rate, and months open and echo back
  45. cout << "Please enter your starting balance. $";
  46. cin >> startingBalance;
  47. if (startingBalance < 0){
  48. cout << "Invalid balance. Please enter positive number." << endl;
  49. return 0;
  50. }
  51. cout << startingBalance << endl;
  52. cout << "Please enter the annual interest rate as a decimal (i.e. 10% as .10). ";
  53. cin >> annualInterest;
  54. cout << annualInterest << endl;
  55. cout << "Please enter the amount of months passed since your account was opened. ";
  56. cin >> monthsOpen;
  57. cout << monthsOpen << endl;
  58.  
  59. // cout << "Starting Balance = " << startingBalance << " Annual Interest = " << annualInterest << " Months Open = " << monthsOpen << endl;
  60.  
  61. //calculate monthly interest and set startingBalance
  62. currentBalance = startingBalance;
  63. monthlyInterest = annualInterest / 12;
  64.  
  65. //execute a balance update for monthsOpen months
  66. for (int i = 0; i < monthsOpen; i++){
  67. //prompt user for amount deposited and add it to balance
  68. cout << "How much money was deposited into the account this month? $";
  69. cin >> monthlyDeposit;
  70. //echo back to user
  71. cout << monthlyDeposit << endl;
  72. currentBalance += monthlyDeposit;
  73. totalDeposited += monthlyDeposit;
  74. //prompt user for amount withdrawn and subtract from balance
  75. cout << "How much money was withdrawn from the account this month? $";
  76. cin >> monthlyWithdrawn;
  77. //echo back to user
  78. cout << monthlyWithdrawn << endl;
  79. //input validation
  80. if (monthlyWithdrawn < 0){
  81. cout << "Error: Amount withdrawn cannot be a negative number. Please enter a positive number. " << endl;
  82. return 0;
  83. }
  84. //subtract amount withdrawn from current balance and add monthlyWithdrawn to accumulator
  85. currentBalance -= monthlyWithdrawn;
  86. totalWithdrawn += monthlyWithdrawn;
  87. //calculate amount of dollars in interest accrued, add to account balance and add to accumulator
  88. monthlyInterestAmount = monthlyInterest * currentBalance;
  89. totalInterestEarned += monthlyInterestAmount;
  90. currentBalance += monthlyInterestAmount;
  91. //end the program if the balance drops below 0
  92. if (currentBalance < 0){
  93. cout << "The bank balance is now negative. Terminating the account. Goodbye! " << endl;
  94. return 0;
  95. }
  96. }
  97.  
  98. //display account information to user after monthsOpen months
  99. cout << "Balance information after " << monthsOpen << " months" << endl;
  100. cout << "Ending balance: $" << currentBalance << endl;
  101. cout << "Total amount deposited: $" << totalDeposited << endl;
  102. cout << "Total amount withdrawn: $" << totalWithdrawn << endl;
  103. cout << "Total amount accrued in interest: $" << totalInterestEarned << endl;
  104.  
  105. return 0;
  106. }
Success #stdin #stdout 0.01s 5548KB
stdin
10000
.10
36
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
500
200
stdout
Please enter your starting balance. $10000
Please enter the annual interest rate as a decimal (i.e. 10% as .10). $0.1
Please enter the amount of months passed since your account was opened.36
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
How much money was deposited into the account this month? $500
How much money was withdrawn from the account this month? $200
Balance information after 36 months
Ending balance: $26120.8
Total amount deposited: $18000
Total amount withdrawn: $7200
Total amount accrued in interest: $5320.82