Trouble with a for loop and display in program

Hello,
have to write a program to calculate the monthly, balance and interest paid on a balloon mortgage.

The program must use a for loop to process the payments, and the results must display in a table with the final (balloon) payment showing on the last line.

Output should look like this:

Please enter your mortgage amount: 100000
Please enter your APR: 5
Please enter your payment amount: 1000
Please enter the number of years of your loan: 5
Month Balance Payment Interest
1 100000.00 1000.00 412.50
2 99412.50 1000.00 410.05
loops...to month 60
Balloon Payment: 60046.43

I need it to display the updated calculations until the final payment is reached. Also the table is indented for some reason. Any help about how to get this functional would be greatly appreciated.

Here is what I have thus far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip> 

using namespace std;
int main()
{
	double mortAmount, payment, apr, years, months;	

	int paymentCounter;
	
	cout << "Please enter your mortgage amount: ";
		cin >> mortAmount; //gets mortgrage amount from user.
		
	cout << "Please enter your APR: "; 
		cin >> apr; //gets annual interest rate from user.
		apr = (apr/100); // converts apr rate to decimal.
		double mApr = apr/12; //converts apr to monthy apr

	cout << "Please enter your payment amount: "; 
		cin >> payment; //gets payment amountt from user.

	cout << "Please enter number of years of your mortgage: "; 
		cin >> years; // gets number of years from the user.
		
	double intPaid = (mortAmount - payment) * apr/12; // calculates interest paid on mortgage.
	double mortBalance = (mortAmount - payment) + intPaid; // calculates remaining mortgage balance.
	int numPayments = years * 12;

	cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2); //formats output with decimal two places.
	cout << setw(8) << "Month" << setw(9) << "Balance" << setw(10) << "Payment" << setw(11) << "Interest" << "\n";
	
	for (paymentCounter = 1; paymentCounter <= numPayments; paymentCounter++) // loop counts the number of months
	
	//table output begins
	
	cout << setw(8) << paymentCounter << setw(9) << mortBalance  << setw(10) << payment << setw(11) << intPaid <<"\n"; 
}


Last edited on
@abby11
setw(?) acts like a tab function, in that it moves the text in the amount inside the parenthesis. Here is your program, showing the monthly payments, etc. In your for loop, you never had the payment being subtracted from the mortAmount. I added a Mortgage function to that. Hope it's what you needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Mortgage Payments.cpp : Defines the entry point for the console application.
//

#include "stdafx.h" // used because I have MS Visual C++ 2008 Express. Leave out if you don't
#include <iostream>
#include <iomanip> 

using namespace std;

double Mortgage(int paymentCounter, double mortamount , double payment, double interest, double apr)
{
	double intPaid = (mortamount - payment) * apr/12; // calculates interest paid on mortgage.
	double mortBalance = (mortamount - payment) + intPaid; // calculates remaining mortgage balance.
	cout << setw(8) << paymentCounter << setw(9) << "$" <<mortBalance  << setw(10) << "$" << payment << setw(11) << "$" << intPaid <<"\n";
	return mortBalance;
}


int main()
{
	double mortAmount, payment, apr, years, months;	

	int paymentCounter;

	cout << "Please enter your mortgage amount: ";
	cin >> mortAmount; //gets mortgrage amount from user.

	cout << "Please enter your APR: "; 
	cin >> apr; //gets annual interest rate from user.
	apr = (apr/100); // converts apr rate to decimal.
	double mApr = apr/12; //converts apr to monthy apr

	cout << "Please enter your payment amount: "; 
	cin >> payment; //gets payment amount from user.

	cout << "Please enter number of years of your mortgage: "; 
	cin >> years; // gets number of years from the user.

	double intPaid = (mortAmount - payment) * apr/12; // calculates interest paid on mortgage.
	double mortBalance = (mortAmount - payment) + intPaid; // calculates remaining mortgage balance.
	double numPayments = years * 12;

	cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2); //formats output with decimal two places.
	cout << setw(8) << "Month" << setw(17) << "Balance" << setw(16) << "Payment" << setw(16) << "Interest" << "\n";

	for (paymentCounter = 1; paymentCounter <= numPayments; paymentCounter++) // loop counts the number of months
		//table output begins
	{
		if (mortAmount >= 0)
		mortAmount = Mortgage( paymentCounter,  mortAmount,  payment, apr, mApr);
	}
	if (mortAmount > 0)
	{
		cout << "Final balloon payment of $" << mortAmount << " is due." << endl;
	}
	else
	{
		cout << "You are due $" << abs(mortAmount) << ", for over payment of the mortgage amount." << endl;
	}
	return 0;
}
Last edited on
Thank you works well I just need the output to also display the first mortgage amount on the first line of output as well for example:

Month Balance Payment Interest
1 100000.00 1000.00 412.50
2 99412.50 1000.00 410.05

right now it displays this:

1 $99412.50 $1000.00 $412.50
2 $98822.55 $1000.00 $410.05

I made a change to the calculation to get the monthly rates to show, but how to get the balance to show the first balance as well?

Here is the update:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip> 

using namespace std;

double Mortgage(int paymentCounter, double mortamount , double payment, double interest, double apr)
{
	double intPaid = (mortamount - payment) * apr/12; 
	double mortBalance = (mortamount - payment) + intPaid; 
	cout << setw(8) << paymentCounter << setw(9) << "$" <<mortBalance  << setw(10) << "$" << payment << setw(11) << "$" << intPaid <<"\n";
	return mortBalance;
}

int main()
{
	double mortAmount, payment, apr, years, months;	

	int paymentCounter;

	cout << "Please enter your mortgage amount: ";
	cin >> mortAmount; //gets mortgrage amount from user.

	cout << "Please enter your APR: "; 
	cin >> apr; //gets annual interest rate from user.
	apr = (apr/100); // converts apr rate to decimal.
	double mApr = apr/12; //converts apr to monthy apr

	cout << "Please enter your payment amount: "; 
	cin >> payment; //gets payment amount from user.

	cout << "Please enter number of years of your mortgage: "; 
	cin >> years; // gets number of years from the user.

	double intPaid = (mortAmount - payment) * apr/12; // calculates interest paid on mortgage.
	double mortBalance = (mortAmount - payment) + intPaid; // calculates remaining mortgage balance.
	double numPayments = years * 12;

	cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2); 
	cout << "Month" << setw(15) << "Balance" << setw(10) << "Payment" << setw(11) << "Interest" << "\n";

	for (paymentCounter = 1; paymentCounter <= numPayments; paymentCounter++) 
		//table output begins
	{
		mortAmount = Mortgage( paymentCounter,  mortAmount,  payment, intPaid, apr);
	}
		if (mortAmount > 0)
			cout << "Balloon payment: " << mortAmount << endl;

}


Last edited on
@abby11

Replace the double Mortgage routine with this one. It's the same routine except the mortBalance calculation comes after the cout.
1
2
3
4
5
6
7
8
double Mortgage(int paymentCounter, double mortamount , double payment, double interest, double apr)
{
	double mortBalance,intPaid;
	intPaid = (mortamount - payment) * apr/12; // calculates interest paid on mortgage.
	cout << setw(8) << paymentCounter << setw(9) << "$" <<mortamount  << setw(10) << "$" << payment << setw(11) << "$" << intPaid <<"\n";
	mortBalance = (mortamount - payment) + intPaid; // calculates remaining mortgage balance.
	return mortBalance;
}


Actually, you don't need the double interest to be passed to the routine. Remove it from the call and routine itself.
Last edited on
Works great now thanks again.
You're welcome...
Topic archived. No new replies allowed.