0

I am having trouble understanding why my function won't work. I've looked at the while loop several times, but I don't see why the program isn't executing correctly. How do I stop the while loop going off infinitely?

I am trying to create a program that tells the user how long it'll take to pay off a loan and what needs to be paid back on the last month. The user types in the loan being borrowed, the interest and the money s/he intends to pay each month.

For example, I borrowed $100 at 12% annual interest. The first month I have to pay $100*0.01 = $1. Let us say that I pay $50 per month then my new balance is 100 + 1 - 50 = $51. Now I have to pay 1% of this which is $0.51. My new balance is 51 + 0.51 - 50 = $1.51 and I keep going until it's all paid off.

This is what my code looks like:

#include <iostream>
using namespace std;
void get_input(double &principal, double &interest, double &payment);

int main()
{
    double money, i, pay;


    cout << "How much do you want to borrow ?";
    cin >> money;
    cout << "What is the annual interest rate expressed as a percent?";
    cin >> i;
    cout << "What is the monthly payment amount?";
    cin >> pay;

    i = i/100; 

    get_input(money, i, pay);

}

void get_input(double &principal, double &interest, double &payment)
{
    double totalpayment, add = 0;
    int months = 1;

while(principal>0)
{
    add = principal*interest;
    principal = principal + add - payment;
    months++;
}

    totalpayment = payment+principal;
    cout << "The last month for your debt to be paid off is: " << months << endl;
    cout << "Your final payment is: " << totalpayment << endl; 

}
Dahaka
  • 507
  • 1
  • 4
  • 14
LorrJ
  • 41
  • 3
  • 7
  • What are the exact inputs you typed into this program? – user2357112 Jul 11 '13 at 00:15
  • I entered 1000 for the loan, 18 for interest and 50 paid monthly – LorrJ Jul 11 '13 at 00:17
  • Exact as in what you literally typed. I doubt you put in dollar or percent signs. – user2357112 Jul 11 '13 at 00:18
  • 1
    Adding this `std::cout << add << ":" << principal << ":" << interest << ":" << std::endl;` to the top of your `while` loop would be very instructive. If this is annual interest rate you also need to divide the interest by `12`. – Shafik Yaghmour Jul 11 '13 at 00:23
  • Shafik what is std:: used for? I've seen it in many places though it's never been explained to me – LorrJ Jul 11 '13 at 00:28
  • @LorraineJane See this previous post [Why is “using namespace std;” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). `cout` and `endl` are in the `std` namespace and usually it is better to use `std:cout` etc... it will save you trouble later on. – Shafik Yaghmour Jul 11 '13 at 00:30
  • @LorrJ why did you just remove all your code? – Dahaka Jul 11 '13 at 06:30

2 Answers2

0

You need to divide percentages by 100 before you use them:

while(principal>0)
{
    add = principal*interest;//this multiplies the number by 18 instead of .18
    principal = principal + add - payment;
    months++;
}

You need to multiply by the percentage - and then divide by 100.

add=principal*interest/100.0;

Additionally, if you pay 18% monthly interest on a $1000 dollar loan, you better pay more than $180 a month if you ever want to pay it off. Try some input that makes sense (like your example) or add some code to test whether your remaining debt has gone down.

Jakob Weisblat
  • 7,450
  • 9
  • 37
  • 65
0

Have you used a debugger yet? If it were me I would set a breakpoint in the while loop and evaluate the variables to see what it's doing. I find debugging code a lot easier to do than reading it and being my own debugger. :)

John Lockwood
  • 3,787
  • 29
  • 27