Hi All,

I am a student and this is a project I am working on. The main idea is that this program will calculate the number of months it will take to pay off a loan. There are catches in place to only allow positive entries and to check to make sure the monthly payment is sufficient to cover the interest on the loan.

Here's the problem: I can't get it to execute properly and I'm pretty sure it's because the loop in the last function is all wrong. I'm starting to think in circles, so I've decided to ask for help. I don't think I'm initializing the expression correctly and I don't know how to make the counter work. Any help would be much appreciated!

Thanks,
Ryan

``````// This program will calculate how long it will take to pay off a loan
// making monthly payments.
#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototypes:
void get_info(double &,double &, double &);
void check_payment(double, double, double);
void pay_loan(double, double, double);

int main()
{
double loan_amount;
double interest_rate;
double monthly_payment;

get_info(loan_amount, interest_rate, monthly_payment);
check_payment(loan_amount, interest_rate, monthly_payment);
pay_loan(loan_amount, interest_rate, monthly_payment);

system("pause");
return(0);
}

//********************************************************************
// Definition of function get_info:  This function will ask the user *
// to input a loan amount, interest rate, and monthly payments.      *
//********************************************************************

void get_info(double &loan_amount, double &interest_rate, double &monthly_payment)
{
cout << "** Welcome to the Consumer Loan Calculator **" <<endl << endl;
cout << "How much do you want to borrow? ";
cin  >> loan_amount;

while (loan_amount <= 0)
{
cout << "You must enter a positive Number!\n";
cout << "How much do you want to borrow? ";
cin  >> loan_amount;
}

cout << "What is the annual interest rate expressed as a percent? ";
cin  >> interest_rate;

while (interest_rate <= 0)
{
cout << "You must enter a positive number!\n";
cout << "What is the annual interest rate expressed as a percent? ";
cin  >> interest_rate;
}

cout << "What is the monthly payment amount? ";
cin  >> monthly_payment;

while (monthly_payment <= 0)
{
cout << "You must enter a positive number!\n";
cout << "What is the monthly payment amount? ";
cin  >> monthly_payment;
}
}

//******************************************************************
// Definition of function check_payment:  This function calculates *
// the user's minimum payment and checks it against the montly      *
// payment entered to make sure it is enough to pay off the loan.  *
//******************************************************************

void check_payment(double loan_amount, double interest_rate, double monthly_payment)
{
double first_interest;
double minimum_payment;

first_interest = (interest_rate / 12) * .01;
minimum_payment = loan_amount * first_interest;

cout << fixed <<showpoint <<setprecision(2);

if (minimum_payment >= monthly_payment)
{
cout << "You must make a payment of at least \$" << minimum_payment + 1 <<endl;
cout << "Because your monthly interest is \$" << minimum_payment;
exit(0);
}
}

void pay_loan(double loan_amount, double interest_rate, double monthly_payment)
{
double interest;
double initial_loan_amount;
double final_loan_amount;
double total_interest = 0;
int month = 0;

for( ; monthly_payment >= initial_loan_amount ; )
{
interest = (interest_rate / 12) * .01;
initial_loan_amount = loan_amount + (loan_amount * interest);
total_interest = total_interest + interest;
final_loan_amount = initial_loan_amount - monthly_payment;
month ++;

cout << "Your debt will be paid off after " << month << " months, ";
cout << "with a final payment of just \$" << final_loan_amount <<endl;
cout << "The total amount of interest you will pay during that time is \$ ";
cout << total_interest << endl << endl;
cout << "** Don't get overwhelmed with debt! **\n";
}
}``````

Here is the formula for calculating the monthly payment

``````int main(){
float loanAmt;
float interestRate;
int   Months;
float MonthlyPayment;
cout << "Enter load amount ...";
cin >> loanAmt;

cout << "Enter interest rate (e.g. 6.5) ...";
cin >> interestRate;

cout << "Enter number of months ...";
cin >> Months;
cin.ignore();

interestRate /= 1200.0;
MonthlyPayment = (pow((1.0F + interestRate), Months)) - 1.0F;
MonthlyPayment = interestRate / MonthlyPayment;
MonthlyPayment = (interestRate + MonthlyPayment) * loanAmt;
cout << "Monthly payment is " << MonthlyPayment << "\n";

}``````

Thanks for the reply, but I'm afraid that doesn't help. I need to keep everything in my program the way it is. I just need help with the loop.

First off, `for(;conditional;)` is the same as `while(conditional)` . So you can fix that. Also, initial_loan_amount is being declared but not initialized (i.e., its filled with nonsense). Fix that.

Hmmm... tried this:

``````void pay_loan(double loan_amount, double interest_rate, double monthly_payment)
{
double interest;
double total_loan_amount = loan_amount;
double total_interest = 0;
double initial_loan_amount = 0;
int month = 0;

while(monthly_payment >= total_loan_amount)
{
month ++;
interest = (interest_rate / 1200) * initial_loan_amount;
initial_loan_amount = total_loan_amount + interest;
total_interest = total_interest + interest;
total_loan_amount - monthly_payment;

}
cout << "Your debt will be paid off after " << month << " months, ";
cout << "with a final payment of just \$" << total_loan_amount <<endl;
cout << "The total amount of interest you will pay during that time is \$ ";
cout << total_interest << endl << endl;
cout << "** Don't get overwhelmed with debt! **\n";
}``````

...still not working the way I'd like it to. When compiled, results show zeros for all values including month.

?????

another appraoch with the same results:

``````void pay_loan(double loan_amount, double interest_rate, double monthly_payment)
{
int month = 0;
double interest;
double debt = loan_amount;
double total_interest = 0;

while(monthly_payment >= debt)
{
month ++;
interest = (interest_rate / 1200) * debt;
total_interest += interest;
debt = loan_amount + interest;
debt -= monthly_payment;

}
cout << "Your debt will be paid off after " << month << " months, ";
cout << "with a final payment of just \$" << debt <<endl;
cout << "The total amount of interest you will pay during that time is \$ ";
cout << total_interest << endl << endl;
cout << "** Don't get overwhelmed with debt! **\n";
}``````

This is what I mean by thinking in circles!

``````void pay_loan(double loan_amount, double interest_rate, double monthly_payment)
{
int month = 0;
double interest;
double debt = loan_amount;
double total_interest = 0;

while(monthly_payment >= debt)
{
month ++;
interest = (interest_rate / 1200) * debt;
total_interest += interest;
debt = loan_amount + interest;
debt -= monthly_payment;

}
cout << "Your debt will be paid off after " << month << " months, ";
cout << "with a final payment of just \$" << debt <<endl;
cout << "The total amount of interest you will pay during that time is \$ ";
cout << total_interest << endl << endl;
cout << "** Don't get overwhelmed with debt! **\n";
}``````

Line 14 looks wrong to me. I think it should be this:

``debt = debt + interest;``

Otherwise you are never ever going to pay down your debt as far as I can tell. If I buy a \$500,000 house, loan_amount will be 500,000, and so my debt is going to keep getting reset at \$500,000 no matter how many payments I make, so you end up with an infinite loop.

Thanks All,

I figured it out - Vernon, you are correct. That line was all wrong. Also, pre-testing this loop was causing me all sorts of problems. Here's the final (working) version:

``````void pay_loan(double loan_amount, double interest_rate, double monthly_payment)
{
int month = 0;
double interest;
double debt = loan_amount;
double total_interest = 0;

do
{
month ++;
interest = (interest_rate / 1200) * debt;
total_interest += interest;
debt += interest;
debt -= monthly_payment;

}
while(debt >= monthly_payment);

// Calculate final payment and total interest amount:
interest = (interest_rate / 1200) * debt;
total_interest += interest;
debt += interest;