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