-
Practice Problem Help!
Ok, so what I have to do is get the interest rate, monthly pay, remaining balance, and interest paid for each month for 12 months. But when I execute the code, I get the all of those things for month 1 but its the same for all 12 months (Doing a For Loop). I dont know how to even get the 4 new things for all 12 months. I spent an hour trying to figure this out LOL. (Started c++ programming a week and a half ago). Heres the code:
Code:
float monthly_rate = .02;
float anual_rate = .2;
float balance = 4800;
float length = 12;
for (int month = 2; month != 13; ++month)
{
cout << "Month: " << month << endl;
float monthly_pay = monthly_rate * balance;
float interest_paid = anual_rate / length * balance;
float principal_paid = monthly_pay - interest_paid;
float remaining_balance = balance - principal_paid;
cout << "Monthly Payment is: " << monthly_pay << endl;
cout << "Interest Paid is : " << interest_paid << endl;
cout << "Principal Paid is: " << principal_paid << endl;
cout << "Remaining Balance is: " << remaining_balance << endl;
}
I tried puting "new balance" and "new monthly pay" as variables and put the same equations in thinking the loop will keep storing the new numbers in them but that didnt work either. What am I doing wrong?
-
Code:
float monthly_pay = monthly_rate * balance;
"monthly_rate" and "balance" are initialized before the loop, and their values never change inside of the loop, therefore the product of these two values will be the same for each loop iteration.
Code:
float interest_paid = anual_rate / length * balance;
float principal_paid = monthly_pay - interest_paid;
float remaining_balance = balance - principal_paid;
These values also remain static for each iteration of the loop.
Perhaps what you're looking for is to incorporate the variable that is being changed for each iteration of the loop into the equations; then values will change each time the loop cycles.
-
This is where it falters at:
Code:
float remaining_balance = balance - principal_paid;
try changing it to something like:
Code:
balance -= principal_paid;
/*---*/
cout << "Remaining Balance is: "<< balance << endl;
So your balance gets deducted every month (remaining balance). Also put a check to see whether your balance has hit 0, so as to stop the loop.