This post has been edited by ssonju: 17 April 2009 - 06:58 AM
Mortgage Amortization Table
Page 1 of 12 Replies - 2415 Views - Last Post: 17 April 2009 - 04:14 PM
Replies To: Mortgage Amortization Table
#2
Re: Mortgage Amortization Table
Posted 17 April 2009 - 10:22 AM
Edit:
code cleaned-up a little ... using some functions to make program more modular and using some input data validation to handle some invalid input
Shalom,
David
http://developers-he.../index.p...opic,127.0.html
code cleaned-up a little ... using some functions to make program more modular and using some input data validation to handle some invalid input
Shalom,
David
http://developers-he.../index.p...opic,127.0.html
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// to make your updates easier ... place these here in global program scope
const int TABLE_YRS[] = {30, 15, 30, 40, 30, 7};
const double INT_RATES[] = {3.50, 6.25, 7.325, 5.50, 8.125, 10.25};
bool more() // defaults to 'yes' ... i.e. must enter 'n' or 'N' for 'No'
{
int reply;
cout << "\nMore ... y/n ? " << flush;
reply = cin.get();
cin.sync(); // flush cin stream ...
if(reply == 'n' || reply == 'N' )
return false; // No more
// else ... if you reach here ...
return true; // Yes more ...
}
char showMenuGetOption() // uses global arrays TABLE_YRS[], INT_RATES[]
{
char option ='0';
// Prompts for Menu Selection ...
for( int i = 0; i<6; ++i)
cout << i+1<< ". " << setw(2) << TABLE_YRS[i] << " years at "
<< setw(5) << INT_RATES[i] << "%\n";
// User's input for mortgage amount & menu option
cout << "\nWhich loan option would you like? ";
cin >> option;
cin.sync();
int T = int(option-'1'); // T is TABLE_YRS/AND INT_RATES array subscript
cout << "You have chosen a ";
char message[] = " year loan with an interest rate of ";
switch (option)
{
case '1': cout << TABLE_YRS[T] << message << INT_RATES[T] << "%\n";
break; // You chose 30 years at 3.50%
case '2': cout << TABLE_YRS[T] << message << INT_RATES[T] << "%\n";
break; // You chose 15 years at 6.25%
case '3': cout << TABLE_YRS[T] << message << INT_RATES[T] << "%\n";
break; // You chose 30 years at 7.325%
case '4': cout << TABLE_YRS[T] << message << INT_RATES[T] << "%\n";
break; // You chose 40 years at 5.50%
case '5': cout << TABLE_YRS[T] << message << INT_RATES[T] << "%\n";
break; // You chose 30 years at 8.125%
case '6': cout << TABLE_YRS[T] << " year loan with an interest rate of "
<< INT_RATES[T] << "%" << endl;
break; // You chose 7 years at 10.25%
default: cout << "option NOT in the valid range 1..6\n"
<< "Please choose a valid option. \n";
return '0'; // Error flag value ...
} // end switch ...
return option;
}
void showLine( int Year, int forLoop, double mthAmtPd,
double mthPrinPay, double mthPayment, double totMortAmt )
{
cout << setw(4) << Year // Year
<< setw(6) << forLoop // Month
<< setw(14)<< mthAmtPd // interest
<< setw(15)<< mthPrinPay // Principle
<< setw(16)<< mthPayment // Payment
<< setw(17)<< totMortAmt // Balance
<< endl;
}
double getDouble( char prompt[] )
{
double mortgageAmount;
for( ;; ) // loop forever until return with a good number ...
{
cout << prompt;
cin >> mortgageAmount; // User's input for mortgage amount
if( !cin.good() )
{
cin.clear();
cin.sync();
cout << "Error! Enter numbers only ...\n";
continue;
}
cin.sync();
return mortgageAmount;
}
}
int main()
{
// set to show 2 decimal places ...
cout << setprecision(2) << fixed;
do
{
char option = showMenuGetOption();
if( option <'1' || option > '6' )
continue; // jump to 'while( ... );' at the end right now
int T = int(option-'1');
char prompt[] =
"What is the amount you are borrowing (number without commas)? ";
double princAmount = getDouble(prompt);
// Amount you are borrowing ... (the principal)
double monthlyInterestRate = INT_RATES[T]/1200;
int totalMonths = TABLE_YRS[T]*12;
// Mortgage Payment Calculator
double mthPayment = princAmount * monthlyInterestRate /
(1 - pow((1 + monthlyInterestRate), -totalMonths));
cout << "\nYour monthly mortgage payment is $" << mthPayment << endl << endl;
cout << "Would you like to view an amortization table? (Y/N) ";
char amortTable;
cin >> amortTable; // User's input for mortgage amount
cin.sync();
// only if y or Y entered ... will the if ... block here get executed
bool monthlyFlag = false; // declare here ... (needs this scope)
if(amortTable == 'y' || amortTable == 'Y')
{
char view = '2'; // default to yearly on entry error ...
cout << "\nWould you like to view monthly or yearly amortization? "
<< "\n1. Monthly" // Option 0 = 30 years at 3.50%
<< "\n2. Yearly\n"; // Option 1 = 15 years at 6.25%
cin >> view;
cin.sync();
if(view == '1') // Monthly Mortgage Amortization Routine
{
cout << "Monthly Routine" << endl;
monthlyFlag = true;
}
// End if loop for Monthly Amortization routine
int Year = 1;
// for loop calculates & displays Balance, Principal Paid,
// & interest Paid every 12 months until Balance is zero
for(int forLoop = 1; forLoop <= totalMonths; forLoop++)
{
if( monthlyFlag && (forLoop)%12 == 1 )
cout << "Year Month forInterest forPrinciple mnthlyPayment"
<< " presentBalance"
<< endl;
else if( forLoop%12 == 1 && Year%12 == 1 )
cout << "\nYear Month forInterest forPrinciple mnthlyPayment"
<< " presentBalance"
<< endl;
// Code generates the current monthly interest and the amount for
// the month to be applied to the principal
double mthAmtIntPd = princAmount * monthlyInterestRate;
double mthPrinPay = mthPayment - mthAmtIntPd;
// Code checks to see if last payment ...
// if so ... pay off prev. months balance + new interest ...
if( forLoop == totalMonths )
{
mthPayment = princAmount + mthAmtIntPd;
mthPrinPay = princAmount;
princAmount = 0;
}
else // Code generates the new principal or new mortgage amount
{
princAmount = princAmount - mthPrinPay;
}
// Code creates a condition that will display the month
// and mortgage balance once every 12 months
if( !monthlyFlag )
{
if( forLoop%12 == 0 )
{
showLine( Year, forLoop, mthAmtIntPd, mthPrinPay,
mthPayment, princAmount );
++Year;
}
}
else
{
showLine( Year, forLoop, mthAmtIntPd, mthPrinPay,
mthPayment, princAmount );
if(forLoop%12 == 0)
{
++Year;
cout << "\nPress 'Enter' to continue ... " << flush;
cin.sync();
cin.get();
} // end if
} // end if/else
} // End for loop for Yearly Mortgage Amortization routine
} // End if loop for Amortization Table or not routine
if( !monthlyFlag )
{
cout << "\nPress 'Enter' to continue ... " << flush;
cin.sync();
cin.get();
}
}while( more() );
} // End of Main
This post has been edited by David W: 17 April 2009 - 09:32 PM
#3
Re: Mortgage Amortization Table
Posted 17 April 2009 - 04:14 PM
Davids correct on that.It does need to be more modular.
Have a look thought it I spotted 2 mistakes I think.I CBA Looking what line and stuff
Have a look thought it I spotted 2 mistakes I think.I CBA Looking what line and stuff
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|