Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Update the variables of my table each time z is less or equal to n

+2 votes
asked Oct 27, 2020 by Jan Rodríguez (150 points)

I just need to update the variables of the table each time z is less or equal to n. That is the only help I need ): because it's printing the same value and I know why but I don't know how to update these variables again. Thanks in advance.

The testing I use is

Loan = 10000

Interest = 12

Payments in months = 15

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    
    //Variables
    float l, air;
    int n, z = 1, count, mainInterest = 100;
    double countingBalance, previousBalance, countingPrincipal, counterPrincipal, interestCounter, interestfinal = 6.50, mainPrincipal = 6.21;
    
    //Inputs of the user
    cout << "Enter loan amount: " << endl;
    cin >> l;
    cout << "Enter annual interest rate: " << endl;
    cin >> air;
    cout << "Enter number of payments in months: " << endl;
    cin >> n;
    
    //Calculation
    float rate = air / 12.00;
    rate = rate / 100;
    float monthlyPayment = (rate * pow(1 + rate, n) *l) / (pow(1 + rate, n) -1);
    float payment = monthlyPayment * n;
    float interestPaid = payment - l;
    double principal = monthlyPayment - 100;
    double endingBalance = l - principal;
    
    //Output
    cout << "Monthly Payment: $" << monthlyPayment << endl;
    cout << "Amount paid back: $" << payment << endl;
    cout << "Interest paid: $" << interestPaid << endl;
    
    cout << "\n----------------------------------Amortization Schedule----------------------------------------" << endl;
    cout << "\tBeginning Balance" << "\tInterest" << "\tPrincipal" << "\tEnding Balance" << endl;
    cout << z << "\t     $" << l << "\t        $100" << "\t        $" << principal << "\t      $" << endingBalance << endl;
    
    //Updating the table - Variables to update the table
    countingPrincipal = principal + mainPrincipal;
    countingBalance = l - countingPrincipal;
    interestCounter = mainInterest - interestfinal;
    previousBalance = countingBalance - principal;
    counterPrincipal = principal + mainPrincipal;
 
    while (z <= n) {
    count = z++;    
    cout << z << "\t     $" << countingBalance << "\t        $" << interestCounter << "\t        $" << counterPrincipal << "\t      $" << previousBalance << endl;
    }
 
    return 0;

}

1 Answer

+1 vote
answered Oct 28, 2020 by Peter Minarik (84,720 points)

I'm rather tired right now to completely understand the maths you're doing.

However, from the programming side of the problem, all you have to do is put your calculation inside the while loop. Use your input from the previous calculations (such as balance) and calculate the values based on this. Maybe it's best if you create a function that calculates and prints the values based on the input. And you just keep calling this function with the right input in every iteration.

commented Oct 28, 2020 by Jan Rodríguez (150 points)
Thank you! I'll do that.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...