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.

Dani AI

Generated

The output was reversed because the program printed the loop counter after decrementing it, so the first line shown corresponds to the last month. The loop condition shown in the original post also contains a logic bug: using a test like (numMonths <= 100) while decrementing numMonths will not stop the loop as intended. C++ is case-sensitive as well, so mismatched names such as Balance vs balance or Payment vs payment will cause compile errors. As suggested, the simplest fix for this assignment is to iterate an ascending month counter (1..N) and keep the original total-months value separate.

A robust approach:

  • read price (double) and totalMonths (int);
  • compute a fixed monthlyPayment = price / totalMonths;
  • loop for (int month = 1; month <= totalMonths; ++month) and compute the balance before the payment (or print then subtract);
  • adjust the final payment to absorb any rounding remainder so the loan hits zero exactly;
  • format money with std::fixed and std::setprecision(2).

Example (compact, complete):

#include <iostream>
#include <iomanip>

int main() {
double price;
int totalMonths;
if (!(std::cin >> price >> totalMonths) || totalMonths <= 0) return 0;

double monthly = price / totalMonths;
std::cout << std::fixed << std::setprecision(2);

for (int m = 1; m <= totalMonths; ++m) {
    double balanceBefore = price - monthly * (m - 1);
    double payment = (m == totalMonths) ? (price - monthly * (m - 1)) : monthly;
    std::cout << m << "\t" << balanceBefore << "\t" << payment << "\n";
}

}

Notes: avoid modifying the input totalMonths directly when it is needed for calculations; prefer int for counts; guard against division-by-zero; use storage (vector/array) only when the schedule must be revisited or sorted. This addresses the issues reported by and follows the simpler Option 2 recommended by .

Recommended Answers

All 5 Replies

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.

im a little confused

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.