0

Here is my code for the same exact program. I have everything working except for displaying the calculated monthly interest. I could use some help.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    double mortgage, interest, payment, years, monthlyInterest, monthlyBalance, numPayments;
    int counter;

    cout << "Please enter a mortgage amount: ";
    cin >> mortgage;
    cout << "Please enter an annual interest rate: ";
    cin >> interest;
    cout << "Please enter a payment amount: ";
    cin >> payment;
    cout << "Please enter number of years for the loan: ";
    cin >> years;
    cout << "Month" << '\t' << "Balance" << '\t' << '\t' << "Payment" << '\t'<< '\t' << "Interest" << endl;
    numPayments = (years * 12);
    interest = interest / 100;
    monthlyBalance = 1;
    monthlyInterest = 1;

    for (counter = 1; counter <= numPayments; counter++)
    {
        cout << counter << '\t' << setiosflags(ios::fixed) << setprecision(2) << mortgage << '\t' <<
            payment << '\t' << '\t' << setiosflags(ios::fixed) << setprecision(2) << monthlyInterest << endl;
        mortgage = mortgage - payment;
        monthlyInterest = ((mortgage) * (interest / 12));
        mortgage = mortgage + monthlyInterest;
    }


}

Edited by Nick Evan: Fixed formatting

1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by jpd5066
0

Nevermind, here is the correct code that works for the program.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    double mortgage, interest, payment, years, monthlyInterest, monthlyBalance, numPayments;
    int counter;

    cout << "Please enter a mortgage amount: ";
    cin >> mortgage;
    cout << "Please enter an annual interest rate: ";
    cin >> interest;
    cout << "Please enter a payment amount: ";
    cin >> payment;
    cout << "Please enter number of years for the loan: ";
    cin >> years;
    cout << "Month" << '\t' << "Balance" << '\t' << '\t' << "Payment" << '\t'<< '\t' << "Interest" << endl;
    numPayments = (years * 12);
    interest = interest / 100;
    monthlyBalance = 1;
    monthlyInterest = 1;

    for (counter = 1; counter <= numPayments; counter++)
    {
        cout << counter << '\t' << setiosflags(ios::fixed) << setprecision(2) << mortgage << '\t' <<
            payment;
        mortgage = mortgage - payment;
        monthlyInterest = ((mortgage) * (interest / 12));
        cout << '\t' << '\t' << setiosflags(ios::fixed) << setprecision(2) << monthlyInterest << endl;
        mortgage = mortgage + monthlyInterest;
    }
}

Edited by Nick Evan: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.