hey there. This is Erinn Hansen and I have a quick question about this assignment I am working on. I pretty much have it, except for it's a little backwards. This assignment is:

Write a program which accepts the following user input:

priceOfHouse, numMonths

Print an amortization chart as shown below.

Price: 100000.00
Month    Balance    Payment
100        99000        1000.00
...
2            1000.00    1000.00
1            0                  1000.00

Here is my code that I have:

#include
#include

using namespace std;

int main()
{
        double priceOfHouse, numMonths, payment = 0;
        double balance;
        cout << "Price: "; cin >> priceOfHouse;
        cout << "Number of months: "; cin >> numMonths;
        balance = priceOfHouse;
        payment = priceOfHouse / numMonths - payment;
        cout << "Month \t Balance \t Payment " << endl; do { balance = balance - payment; cout << numMonths << "\t" << Balance << "\t" << Payment << endl; numMonths--; } while ((balance > 0) && (numMonths <= 100))
}

Instead of getting:

Price: 100000.00
Number of months: 100

Month    Balance    Payment
1        100000.00  1000.00
2        99000.00  1000.00
...
100        1000.00  1000.00

I get:

Price: 100000.00
Number of months: 100

Month    Balance    Payment
100        99000        1000.00
...
2            1000.00    1000.00
1            0                  1000.00

Basically it's backwards and want to know how to make it so that it is like this:

Month    Balance    Payment
1        100000.00  1000.00
2        99000.00  1000.00
...
100        1000.00  1000.00

I know it's probably something simple, but can't seem to figure out what is wrong. Could someone help me :) . thanks.

The order that you are calculating is the order that you are displaying. This is because you are not storing earlier calculations. Data must be immediately displayed or it is lost. You need to do one of two things:

Option 1: Set aside storage in an array, vector, or linked list so that you can store more than one payment. Calculate all the payments before displaying any of them. Then go through the array(s)/vector(s)/linked list(s) and display in whatever order you choose. I'd go for an array in this case.

Option 2: Calculate in the order that you want to display. In this case, start with numMonths (which is your counter) at 0 and add to it each time rather than starting at numMonths and subtracting from it each time.

In your case, go for option 2. It's easier. I listed option 1 because if your program gets more complicated, it might come in handy. In this particular case, it's not necessary.

I'm also confused about this while-loop criteria:

while ((balance > 0) && (numMonths <= 100))

numMonths is decreasing. Once numMonths <= 100, it will always be, so that will never kick you out of the loop except possibly after going through the loop the first time.

I tried option 2, but it still doesn't work.

I tried option 2, but it still doesn't work.

Post the code where you tried option 2 so we can see how you did it please.

actually i figured it out my mistake. thank you so much for your help. i appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.