9 Replies - 1564 Views - Last Post: 23 October 2011 - 12:10 PM Rate Topic: -----

#1 abby11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-October 11

Creating a table for mortgage program

Posted 22 October 2011 - 07:01 AM

Hello,

I 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

This is what I have been able to do so far:
#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; //mortgrage amount from user.
		
	cout << "Please enter your APR: "; 
		cin >> apr; //gets annual interest rate from user.
		if(apr >= 1)
		apr = (apr/100); // converts percent to decimal.

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

	cout << "Please enter number of years of your loan: "; 
		cin >> years; // number of years from the user.
		int numPayments = years * 12;

cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2); //formats output with decimal two places.
	
	
	//The table output begins
	cout << "Month\tBalance\tPayment\tInterest" << endl;
	int dividelist = 0;
	for (paymentCounter = 0; paymentCounter <= numPayments; ++paymentCounter)
	{
		//calculations
		double intPaid = (mortAmount - payment) * apr/12; // calculates interest paid on mortgage.
		double mortBalance = (mortAmount - payment) + intPaid; // calculates remaining mortgage balance.

		if (mortBalance < 0)mortBalance = 0;
					mortBalance = mortBalance;
					
					if (dividelist == 0)
						{
							cout << numPayments << "\t" << mortBalance << "\t" << payment<< "\t" <<intPaid << endl;
						}
		
					++dividelist;
	}
}



Lost about how to get the output to display as a table properly and run the length of the for loop showing all months of the loan and the final balloon payment. I'd appreciate any insight on how to get the output working properly.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a table for mortgage program

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Creating a table for mortgage program

Posted 22 October 2011 - 07:13 AM

To print your output as a table you will probably want to investigate the functions available in the iomanip header file, along with the setw() function.

Jim

This post has been edited by jimblumberg: 22 October 2011 - 07:13 AM

Was This Post Helpful? 2
  • +
  • -

#3 abby11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-October 11

Re: Creating a table for mortgage program

Posted 23 October 2011 - 07:04 AM

Hello thank you I made a few adjustments and I get the table to display but I still have a few problems with the for loop. 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.

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


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Creating a table for mortgage program

Posted 23 October 2011 - 07:22 AM

Quote

I need it to display the updated calculations until the final payment is reached.

You will need to do some calculations inside your loop if you want the numbers to change.

Quote

Also the table is indented for some reason

It is indented because you tell it to be indented with the first setw() statement. If you don't want it indented remove the first setw() statement.
cout << setw(8) << paymentCounter << setw(9) << mortBalance  << setw(10) << payment << setw(11) << intPaid <<"\n";


Jim
Was This Post Helpful? 1
  • +
  • -

#5 abby11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-October 11

Re: Creating a table for mortgage program

Posted 23 October 2011 - 08:51 AM

//The table output begins
	cout << paymentCounter << setw(15) << mortBalance  << setw(10) << payment << setw(11) << intPaid <<"\n"; 


Thanks, I was able to fix the indent problem.

Have tried moving the calculations in the for loop itself and then it fails to run in the output. I do not know how to get the balance and the interest to subtract in the loop. Have to use a for loop to calculate the payment. How would I go about getting this to work?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Creating a table for mortgage program

Posted 23 October 2011 - 08:52 AM

Show how you have tried putting the calculations inside the loop.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 abby11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-October 11

Re: Creating a table for mortgage program

Posted 23 October 2011 - 09:03 AM

Here is the attempt at the nested for loop calculation:

for (paymentCounter = 1; paymentCounter <= numPayments; paymentCounter++)
	{
			mortBalance = mortAmount;
			for (mortCounter = 1; mortCounter <= numPayments; mortCounter++)
			{
				mortBalance = (mortAmount - payment) + intPaid;
			}
	}
	//The table output begins
	cout << paymentCounter << setw(15) << mortBalance  << setw(10) << payment << setw(11) << intPaid <<"\n"; 
}


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Creating a table for mortgage program

Posted 23 October 2011 - 09:20 AM

Since you are not using arrays to hold your monthly information you must print the information as you calculate the information. I'm not sure what the purpose is for the second loop. But you must print the information in the loop. Once the information changes you can not print the prior information.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 abby11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-October 11

Re: Creating a table for mortgage program

Posted 23 October 2011 - 09:43 AM

Thank you but would have to nest another loop inside the number of payments loop in order to get the balance to update as each payment is subtracted each month?

I just need it to countdown until it reaches the end of the mortgage loan period and then to show the amount of the final payment. Still a bit confused about how to get it to show the balance changes.
Was This Post Helpful? 0
  • +
  • -

#10 abby11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-October 11

Re: Creating a table for mortgage program

Posted 23 October 2011 - 12:10 PM

With help I was able to get the program to work but I need to know how to also display the first first balance entered by the user on count 1. For example:

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

Right now the output displays this:

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

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

}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1