Loan Calculator Formula Help, C++ Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gillky
    New Member
    • Feb 2014
    • 2

    Loan Calculator Formula Help, C++ Code

    Hello I am very new to programming as you will probably tell by my code, if anyone can help me solve my issue it would be much appreciated. I need to get this formula:


    into c++ code with the variables below and I'm having quite a hard time, as I said, any help is appreciated.

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    	double loanAmount = 25000;
    	double interestRate = 7;
    	double payment = 495.03;
    	double paymentsMade = 12;
    	double loanbalance = pow((loanAmount(1 + interestRate / 12 * 100), paymentsMade)) - (payment / interestRate / 12 * 100)* pow(loanAmount((1 + interestRate / 12 * 100, paymentsMade) - 1));
    Last edited by Rabbit; Feb 8 '14, 11:00 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Code:
    double loanbalance = pow((loanAmount(1 + interestRate / 12 * 100), paymentsMade)) - (payment / interestRate / 12 * 100)* pow(loanAmount((1 + interestRate / 12 * 100, paymentsMade) - 1));
    is too complicated for me to understand either.

    Use smaller steps. Calculate for a payment how much is interest and how much is principal. Do this for one payment Then the loan balance is the current balance minus the part of the payment that was principal.

    You could put those statements in a loop and amortize the loan.

    Readability is a big issue in C++. Choose several simple statements over one big one. Beginners especially should not use the comma operator.

    Break this complex statement into two or three smaller ones.

    Code:
    double interest;
    double CurrentBalance;
    
    CurrentBalance = loanAmount;
    
    interest = CurrentBalance * interestRate;  //for a year
    
    CurrentBalance = CurrentBalance - (payment - interest/12);

    Comment

    Working...