### My program runs infinitely for Mortgage Calculator

I'm using VS to create a Mortgage Calculator. I want the code to stop executing once the loan balance reaches 0 that's why i set the loanBalance > 0 so once the code reaches 0 it stops ( i think). But the code continues infinitely without ever changing the result of my cout.... I tried doing "--" for the balance of everything yet my code still continues to run without ever stop once it reaches 0. I don't really care about the format at this moment, i just want to know why it keeps running without stoping at 0 and how i could fix it. I would also like to know why are my cout variables not changing at all

**If it helps, the data i'm supposed to enter is 100000 for Loan Amount. 6 for Interest rate and 15 for the years.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104`` ``````#include #include using namespace std; int main ( ) { int month = 1; // Holds the user's amount of loan. double loanAmount; // Holds the user's interest rate. double i; // Holds the user's yearly Interest Rate. double interestRate; // Holds the user's interest paid every month. double interestPaid; // "n" means the months it will take the user to pay off the loan. double n; // Holds the variable for total months. double years; // Holds payment data. double monthlyPayment; // Holds the user Principle paid. double principlePaid; // Holds the user Loan balance. double loanBalance; // Asks the user to input how much money they took out. cout<<"Enter loan amount ->"; cin>>loanAmount; // Asks the user to input their interest rate. cout<<"Enter interest rate -->"; cin>>interestRate; i = (interestRate/1200); // Asks the user to input the number of years they are taking the loan for. cout<<"Enter number of years of the laon ->"; cin>>years; n = years*12; // Equation that determines the user's monthly payment. monthlyPayment = loanAmount*(i/(1-pow(1+i,-n))); // Equation for interest paid every month. interestPaid = loanAmount*i; // Equation for Principle paid. principlePaid = monthlyPayment-interestPaid; // Equation for Loan Balance loanBalance = loanAmount-principlePaid; cout<<"Month"<<"\t"<<"Payment"<<"\t"<<"Interest Paid"<<"\t"<<"Principle Paid"<<"\t"<<"Loan Balance"< abs(0)) { cout<
I think that the following loop

 ``12345678910111213`` `````` while (loanBalance > abs(0)) { cout<

will be infinit if the initial value of loanBalance before entering the loop is positive. Also I do not see any sense of using function abs with argument equal to 0.
Last edited on
I see. The abs ( 0 ) came from one of my tries to fix it, it was not on my original code. However, how can i fix the code? I need the loanBalance to keep decreasing at every month. I assumed putting "--" would work and it did, but it went to -inf this time. I'm not sure how to stop the code at 0 and that's what my professor wants
Your problem is that your loanBalance is never changing within the loop. Try adding loanBalance-=principalPaid at the end of the loop so that it can eventually reach 0.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` `````` // Equation that determines the user's monthly payment. monthlyPayment = loanAmount*(i/(1-pow(1+i,-n))); // Equation for interest paid every month. interestPaid = loanAmount*i; // Equation for Principle paid. principlePaid = monthlyPayment-interestPaid; // Initialize loanBalance. loanBalance = loanAmount; cout<<"Month"<<"\t"<<"Payment"<<"\t"<<"Interest Paid"<<"\t"<<"Principle Paid"<<"\t"<<"Loan Balance"<0);``````

With that code that I just showed you, now the problem is the very last payment. The last payment doesn't get shown...at least with the tests I ran. You can include an IF statement that says

 ``123456`` ``````if (loanBalance
Last edited on
You did help out alot with the code you provided. At least my code stops now :) But I'm still having some issues.

If we leave ` loanBalance = loanAmount; `

the output it will give me for the first loanBlanace would be my total which is 100k. Now if i leave as

` loanBalance = loanAmount-principlePaid `

The first output will be fine. However, this is my own mistake. I forgot to add that Interest Paid will be reduced as well. So the `Interest Paid` for month 1 is = 500.00. Month 2 is 498.28. Month 3 = 496.55. The Interest rate you pay a month is decreasing never by the same amount. If i can fix that then i can get my last part done. Any ideas? Also my month's keep increasing in increments of 2. Please help it's due at midnight

****** I figured why this was happening however if anyone can still help me on why my month's are increasing in 2's increments.
Last edited on
Can you post your updated code?
This is my updated code

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110`` ``````#include #include using namespace std; int main ( ) { int month = 1; // Holds the user's amount of loan. double loanAmount; // Holds the user's interest rate. double i; // Holds the user's yearly Interest Rate. double interestRate; // Holds the user's interest paid every month. double interestPaid; // "n" means the months it will take the user to pay off the loan. double n; // Holds the variable for total months. double years; // Holds payment data. double monthlyPayment; // Holds the user Principle paid. double principlePaid; // Holds the user Loan balance. double loanBalance; // Asks the user to input how much money they took out. cout<<"Enter loan amount ->"; cin>>loanAmount; // Asks the user to input their interest rate. cout<<"Enter interest rate -->"; cin>>interestRate; i = (interestRate/1200); // Asks the user to input the number of years they are taking the loan for. cout<<"Enter number of years of the laon ->"; cin>>years; n = years*12; // Equation that determines the user's monthly payment. monthlyPayment = loanAmount*(i/(1-pow(1+i,-n))); // Equation for interest paid every month. interestPaid = loanAmount*i; // Equation for Principle paid. principlePaid = monthlyPayment-interestPaid; // Equation for Loan Balance loanBalance = loanAmount-principlePaid; cout<<"Month"<<"\t"<<"Payment"<<"\t"<<"Interest Paid"<<"\t"<<"Principle Paid"<<"\t"<<"Loan Balance"<0); cin.get ( ); cin.get ( ); return 0; }``````

My last error is that my result in Interest paid doesn't come off exactly as what my professor wants. For the second month, i get 496.56 when it's supposed to be 498.28.
Topic archived. No new replies allowed.