Hey all I'm having trouble with loops again and this time it is for loops. I need to calculate monthly balance and interest paid on a balloon mortgage and am having trouble with the calculations. An example of what the program should look like:

Enter Mortgage: 100000
Enter Interest Rate: 5
Enter Payment Amount: 1000
Enter Number of Years for Loan: 5

Month Balance Payment Interest
1 100000.00 1000.00 412.50
2 99412.50 1000.00 410.05
|
|
|
60 60797.27 1000.00 249.16

Balloon Payment: 60046.43


Here is my code so far:

#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;

	monthlyInterest = ((mortgage - payment) * interest / 12);
	monthlyBalance = ((mortgage - payment) + monthlyInterest);
	numPayments = (years * 12);

	cout << "Month" << '\t' << "Balance" << '\t' << '\t' << "Payment" << '\t'<< '\t' << "Interest" << endl;

	for (counter = 1; counter <= numPayments; counter++)
	{
		cout << counter << '\t' << setiosflags(ios::fixed) << setprecision(2)  << monthlyBalance << '\t' << payment << '\t' << '\t' << setiosflags(ios::fixed) << setprecision(2) << monthlyInterest << endl;
	}
	
	
}

There is nothing wrong with the for loop on line 25. What exactly do you want help with? (hint: you need to do more calculations within that loop. can't be done all on one line.)

When I run the program I get the same value for the Balance and Interest column. I'm not sure if its an error in calculation or the way I have my program set up.

That's because you don't do any calculations within the loop. All you do is display the values you calculated before the loop starts.

Before posting "how do I fix it", think...

I tried putting my calculations inside the for loop and now my program does not run at all. It seems the more I try to correct it the more I get confused...

I tried putting my calculations inside the for loop and now my program does not run at all. It seems the more I try to correct it the more I get confused...

Since we're not the Psychic Programmer's Forum, we can't really help you understand what you did wrong based on the information given.

Sorry for the confusion but I'm not very knowledgeable in programming. I have taken my calculations that were outside of the loop and have placed them inside the loop. Now when I try to run the program all I get is the titles of each column( Month, Balance, etc.) and then the program ends. Maybe I'm not understanding you clearly but the calculations were correct just not in the loop therefore nothing was being calculated? Sorry again for the confusion and I truly do appreciate your 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;

	for (counter = 1; counter <= numPayments; counter++)
	{
		monthlyInterest = ((mortgage - payment) * interest / 12);
		monthlyBalance = ((mortgage - payment) + monthlyInterest);
		numPayments = (years * 12);

		cout << counter << '\t' << setiosflags(ios::fixed) << setprecision(2) << monthlyInterest << '\t' <<
			payment << '\t' << '\t' << setiosflags(ios::fixed) << setprecision(2) << monthlyBalance << endl;
	}
	
	
}

Edited 5 Years Ago by PSP1202: n/a

First off, stop apologizing! Not everyone was born a programmer. It's OK... :icon_wink:

Second, you need to think about what you are doing. What are the limits of your loop - from what value to what value? Where do the values come from? Where are they now calculated?

Third, when calculating running totals (values in each line or the table), the values used in these calulatons must be modified. What values are you using to calculate these totals and where are they modified?

You might want to sit at a desk (no computer) and actually do a few of the calculations by hand. If you don't understand how the numbers interact with each other, you have no idea how to program the task. What values are computed for a line? Do these computed values affect the next line in the table? How?

You can't just throw equations into code and pray it works. You must understand the concepts in order to write the code properly.

This article has been dead for over six months. Start a new discussion instead.