...

C++ Problem

grifjason
07-12-2007, 02:11 AM
// This program will tell you how many months it will take
// you to pay off the loan, as well the total amount of interest
// paid over the life of the loan.
#include <iostream>
using namespace std;

int main()
{
double loan_amount, interest, amt_left, interest_paid = 0,
interest_rate = .015, last_payment;
int monthly_payment = 50, num_months = 0;

cout << "What is the amount of the loan: ";
cin >> loan_amount;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

while (loan_amount > 0)
{
cout << loan_amount << endl;
interest = interest_rate * loan_amount;
amt_left = monthly_payment - interest;
loan_amount = loan_amount - amt_left;
interest_paid = interest_paid + interest;
num_months++;
}

cout << loan_amount << endl;
cout << num_months << endl;
cout << interest_paid << endl;

return 0;
}
I need help with handling the last payment. I cant figure out if I need another loop or if I can adjust my condition to make it work. There is \$47.12 left over, and I would like to know how to handle it.
Appreciate the help.

grifjason
07-13-2007, 05:26 AM
The program works like it should. That last payment needs to be handled differently. There is a 50 dollar monthly payment being made, except for the last payment. How do I make it go to to zero and count the number of months and add it to the amount of interest paid.

jakbo
08-01-2007, 07:41 PM
you need to put a condition in there that says if the loan amount is less than the monthly payment, the loan payment is equal to the loan amount.

then it should work. also, you should make it so that a user can enter the loan payment and interest rate.

another thing you might want to think about doing is calculating the interest and checking whether or not it's greater than the loan payment. if it is then you need to make the program exit; otherwise it will keep going forever as the loan amount will increase infinitely.

hope that helps.