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";
}
}