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.
#include <iostream>
#include <iomanip>
usingnamespace 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"<<endl;
// Beginning of while coding.
while (loanBalance > abs(0))
{
cout<<month++<<"\t"<<fixed<<setprecision(2)<<monthlyPayment // Code used to display monthly Payment.
<<"\t"<<fixed<<setprecision(2)<<interestPaid // Code used to display Interest Paid.
<<"\t"<<fixed<<setprecision(2)<<principlePaid // Code used to display Principle Paid.
<<"\t"<<fixed<<setprecision(2)<<loanBalance; // Code used to display Loan Balance.
}
cin.get ( ); cin.get ( );
return 0;
}
while (loanBalance > abs(0))
{
cout<<month++<<"\t"<<fixed<<setprecision(2)<<monthlyPayment // Code used to display monthly Payment.
<<"\t"<<fixed<<setprecision(2)<<interestPaid // Code used to display Interest Paid.
<<"\t"<<fixed<<setprecision(2)<<principlePaid // Code used to display Principle Paid.
<<"\t"<<fixed<<setprecision(2)<<loanBalance; // Code used to display Loan Balance.
}
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.
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.
// 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"<<endl;
// Beginning of while coding.
do
{
cout<<month++<<"\t"<<fixed<<setprecision(2)<<monthlyPayment // Code used to display monthly Payment.
<<"\t"<<fixed<<setprecision(2)<<interestPaid // Code used to display Interest Paid.
<<"\t\t"<<fixed<<setprecision(2)<<principlePaid // Code used to display Principle Paid.
<<"\t\t"<<fixed<<setprecision(2)<<loanBalance<<endl; // Code used to display Loan Balance.
// Equation for Loan Balance
loanBalance = loanBalance-principlePaid;
}
while (loanBalance>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
1 2 3 4 5 6
if (loanBalance<monthlyPayment)
{
monthlyPayment=loanBalance;
// Formula for the new principle and the new interest paid for the final payment should go here.
loanBalance=0;
}
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.
#include <iostream>
#include <iomanip>
usingnamespace 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"<<endl;
// Beginning of while coding.
do
{
cout<<month++<<"\t"<<fixed<<setprecision(2)<<monthlyPayment // Code used to display monthly Payment.
<<"\t"<<fixed<<setprecision(2)<<interestPaid // Code used to display Interest Paid.
<<"\t"<<fixed<<setprecision(2)<<principlePaid // Code used to display Principle Paid.
<<"\t"<<fixed<<setprecision(2)<<loanBalance<<endl; // Code used to display Loan Balance.
loanBalance = loanBalance-principlePaid;
interestPaid = loanBalance*i;
principlePaid = monthlyPayment-interestPaid;
}
while (loanBalance>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.