Reputation: 3
Hello this is the purpose of my program:
Write a program that calculates the balance of a savings account at the end of a three month period. It should ask the user for the starting balance and the annual interest rate. A loop should then iterate once for every month int he period, performing the following steps:
A) Ask the user for the total amount deposited into the account during that month and add it to the balance. Do not accept negative numbers.
B) Ask the user for the total amount withdrawn from the account during that month and subtract it from the balance. Do not accept the negative numbers or numbers greater than the balance after the deposits for the month have been added in.
C) Calculate the interest for that month. The monthly interest rate is the annual interest rate divided by 12. Multiply the monthly interest rate by the average of that month's starting and ending balance to get the interest amount for the month. This amount should be added to the balance.
After the last iteration, the program should display a report that includes the following information:
The problem I am having is that at the end when I display the table my total deposits, withdrawals as well as interest amount are only displaying the last instances at the end of the loop and not the total sum over the three months. Here is my code, sorry if its needlessly complex or messy.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//variables
double depositAmount;
double withdrawAmount;
int monthPeriod = 3;
double startBalance;
double finalBalance;
double totalBalance;
double annInterestRate;
double monthInterestRate;
double monthInterestAmount;
double monthAverageBalance;
double monthAverageAmount;
int count;
cout << "What is your starting balance? ";
cin >> totalBalance;
cout << "What is your annual rate? ";
cin >> annInterestRate;
for (count = 1; count <= monthPeriod; count++)
{
cout << "Enter total amount deposited for the month ";
cin >> depositAmount;
while (depositAmount < 0)
{
cout << "Error, no negative amounts, please try again." << endl;
cin >> depositAmount;
}
cout << "Enter total amount withdrawn for the month ";
cin >> withdrawAmount;
while (withdrawAmount < 0 || withdrawAmount > totalBalance)
{
cout << "Error, no negative amounts or withdrawals greater than your balance. Please try again" << endl;
cin >> withdrawAmount;
}
startBalance = totalBalance + depositAmount;
finalBalance = totalBalance - withdrawAmount;
totalBalance = startBalance - finalBalance;
monthInterestRate = annInterestRate * 12;
monthAverageBalance = (startBalance + finalBalance) / 2;
monthInterestAmount = monthAverageBalance * monthInterestRate;
totalBalance = monthInterestAmount + totalBalance;
}
cout << "Your starting balance at the beginning of three months " << startBalance << endl;
cout << "Total deposits over three months " << depositAmount << endl;
cout << "Total withdrawals over three months " << withdrawAmount << endl;
cout << "Total interest posted to account over three months " << monthInterestAmount << endl;
cout << "Final Balance: " << totalBalance << endl;
cout << "Thank you for using the program!" << endl;
return 0;
}
Upvotes: 0
Views: 2031
Reputation: 1622
This seems like a HW problem, so maybe I'll just give a hint. Hint: what variable is accumulating the monthly deposits?
Also, an unsolicited answer: your monthly interest rate is probably not 12*Annual rate, it is probably Annual rate / 12. Hope this helps.
Upvotes: 0