Mar 19, 2012 at 2:02am UTC
In my program, my interest rate is never correctly calculated. I tried a number of things and still cannot get it right. The directions to say to take the months (as you will see in the code) beginning balance & ending balance to calculate the interest. Then, add the interest to the final balance. This is a portion of my code:
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
//Ask user to input interest, starting balance, & months since account
cout<< "Enter the annual interest rate: " ;
cin>>AnnualRate;
cout << "Enter the starting balance: " ;
cin >> StartingBalance;
cout<< "How many months have passed since the account was established? " ;
cin >> Month;
cout << "\n\n" ;
//Divide AnnualRate by 12 to determine MonthlyRate
MonthlyRate = AnnualRate / 12;
TotalBalance = StartingBalance;
//Variables that will help calculate average
double StartBalanceVar , EndBalanceVar;
StartBalanceVar = TotalBalance;
for ( int count = 1; count <= Month; count++ )
{
double Deposit = 0, Withdraw = 0;
cout<< "Month " << count << endl;
//User input for deposits
cout << setw(36) << "Enter the amount deposited: " ;
cin >> Deposit;
//Validations
if ( Deposit < 0 )
{
cout << setw(36) << "The amount deposited cannot be negative. Please enter again: " ;
cin >> Deposit;
}
TotalBalance += Deposit;
TotalDeposits += Deposit;
//User input for withdrawals
cout << setw(36) << "Enter the amount withdrawn: " ;
cin >> Withdraw;
//Validate that the amount withdrawn is not a negative #
if ( Withdraw < 0 )
{
cout << setw(36) << "The amount withdrawn cannot be negative. Please enter again: " ;
cin >> Withdraw;
}
//Validate that amount withdrawn is not higher than current balance
if ( Withdraw > TotalBalance )
{
cout << setw(36) << "Sorry, the amount withdrawn cannot exceed the current balance." ;
cout << setw(36) << "Please enter again: " ;
cin >> Withdraw;
}
TotalBalance -= Withdraw;
TotalWithdrawals += Withdraw;
EndBalanceVar = TotalBalance;
Average = (StartBalanceVar + EndBalanceVar) / 2;
TotalInterest = Average * MonthlyRate;
TotalInterest += TotalInterest;
TotalBalance += TotalInterest;
Could anyone help me pinpoint my problem. Thanks.
- Nela
Last edited on Mar 19, 2012 at 5:05am UTC
Mar 19, 2012 at 2:11am UTC
I think that my program is taking the interest and applying it to the final balance instead of applying it monthly then adding the monthly value up. Could that maybe be the problem? (:
Mar 19, 2012 at 4:16am UTC
MonthlyRate = AnnualRate / 12;
When annual interest rate is entered is it a percentage? If so, you will need to divide by 100.0.
TotalInterest += TotalInterest;
This doubles the interest you just calculated for the month.
Mar 19, 2012 at 4:41am UTC
Thank you. It doesn't specify. It does not specify, but going by the example given on the bottom, I would assume so.
Though I am following these directions:
"calculate the interest for that month. The monthly interest rate is the annual interest rate divided by 12. Mulitply the monthly interest rate by the average of that months starting and ending balance to get the interest amount from that month. This amount should be added to the balance."
My interest accumulated is still wrong.
Here is a sample output:
Beginning Balance: 2000
Interest Rate: 0.002
Month 1:
Deposits:200
Withdrawals: 300
Month 2:
Deposits:300
Withdrawals:200
Month 3 :
Deposits:400
WithDrawals: 300
Ending Balance : 2300.01
Deposits: 900
WithDrawals: 600
Interest Earned: 0.01
The formula given does not make 1 cent of interest possible. I've been at this for three hours.
Thanks vin (:
Last edited on Mar 19, 2012 at 4:43am UTC
Mar 19, 2012 at 4:47am UTC
I tried dividing it by 100 & the balance matched the above example!! :D
However, for some odd reason, TotalInterest is displayed as 0.00 cents.. -.-
thankyou, thankyou!!!
I specified in my program for the user to enter the interest rate in decimal (whole #) form. :)
Mar 19, 2012 at 4:54am UTC
If you see any reason why the totalinterest would be 0 cents while totalbalance is displayed correctly, let me know (:
Mar 19, 2012 at 5:04am UTC
Part of code:
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
//Ask user to input interest, starting balance, & months since account
cout<< "Enter the annual interest rate (in percentage form): " ;
cin>>AnnualRate;
cout << "Enter the starting balance: " ;
cin >> StartingBalance;
cout<< "How many months have passed since the account was established? " ;
cin >> Month;
cout << "\n\n" ;
//Determine MonthlyRate
MonthlyRate = (AnnualRate / 100) / 12;
//Set StartingBalance as TotalBalance
TotalBalance = StartingBalance;
//Variables that will help calculate average
double StartBalanceVar , EndBalanceVar;
for ( int count = 1; count <= MonthLimit; count++ )
{
StartBalanceVar = TotalBalance;
double Deposit = 0, Withdraw = 0;
cout<< "Month " << count << endl;
//User input for deposits
cout << setw(36) << "Enter the amount deposited: " ;
cin >> Deposit;
//Validations
if ( Deposit < 0 )
{
cout << setw(36) << "The amount deposited cannot be negative. Please enter again: " ;
cin >> Deposit;
}
TotalBalance += Deposit;
TotalDeposits += Deposit;
//User input for withdrawals
cout << setw(36) << "Enter the amount withdrawn: " ;
cin >> Withdraw;
//Validate that the amount withdrawn is not a negative #
while ( Withdraw < 0 )
{
cout << setw(36) << "The amount withdrawn cannot be negative. Please enter again: " ;
cin >> Withdraw;
}
//Validate that amount withdrawn is not higher than current balance
while ( Withdraw > TotalBalance )
{
cout << setw(36) << "Sorry, the amount withdrawn cannot exceed the current balance." ;
cout << setw(36) << "Please enter again: " ;
cin >> Withdraw;
}
TotalBalance -= Withdraw;
TotalWithdrawals += Withdraw;
EndBalanceVar = TotalBalance;
//Determine Interest
Average = (StartBalanceVar + EndBalanceVar) / 2;
TotalInterest = MonthlyRate * Average;
TotalBalance += TotalInterest;
}
//Output
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Ending Balance: " << setw(16) << "$" << setw(13) << TotalBalance << endl;
cout << "Amount of Deposits: " << setw(12) << "$" << setw(13) << TotalDeposits << endl;
cout << "Amount of Withdrawals: " << setw(9) << "$" << setw(13) << TotalWithdrawals << endl;
cout << "Amount of interest earned: " << setw(5) << "$" << setw(13) << TotalInterest << endl;
cout << "\n\n" ;
OUTPUT
TotalBalance: 2300.01
...
...
Amount of Interest Earned : 0.00
Hmm.. I really can't see any reason why TotalBalance is displaying correctly and TotalInterest is not... All I did was add the interest to the balance.
Last edited on Mar 19, 2012 at 5:11am UTC
Mar 19, 2012 at 9:48am UTC
Well TotalInterest is really not the total interest but the monthly interest so you are outputting the monthly interest for the last month maybe.
Create a variable MonthlyInterest and add it to TotalInterest which is set to 0 before the for loop.
1 2 3
MonthlyInterest = MonthlyRate * Average;
TotalInterest += MonthlyInterest
TotalBalance += MonthlyInterest;
Also in your sample data should withdrawals for Month 3 be 100 not 300?
Last edited on Mar 19, 2012 at 10:47am UTC