C++: Building functions -
i having trouble understanding why function won't work. i've looked @ while loop several times, don't see why program isn't executing correctly. how stop while loop going off infinitely?
i trying create program tells user how long it'll take pay off loan , needs paid on last month. user types in loan being borrowed, interest , money s/he intends pay each month.
for example, borrowed $100 @ 12% annual interest. first month have pay $100*0.01 = $1. let pay $50 per month new balance 100 + 1 - 50 = $51. have pay 1% of $0.51. new balance 51 + 0.51 - 50 = $1.51 , keep going until it's paid off.
this 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 want borrow ?"; cin >> money; cout << "what annual interest rate expressed percent?"; cin >> i; cout << "what monthly payment amount?"; cin >> pay; = 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 debt paid off is: " << months << endl; cout << "your final payment is: " << totalpayment << endl; }
you need divide percentages 100 before use them:
while(principal>0) { add = principal*interest;//this multiplies number 18 instead of .18 principal = principal + add - payment; months++; }
you need multiply percentage - , divide 100.
add=principal*interest/100.0;
additionally, if pay 18% monthly interest on $1000 dollar loan, better pay more $180 month if ever want pay off. try input makes sense (like example) or add code test whether remaining debt has gone down.
Comments
Post a Comment