#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
float GBegBal, GRate, GPayment;
int GPeriods;
//Prototypes
void DisplaySchedule(float BegBal,float Rate,int Periods,float Payment);
float CalcPayment(float BegBal,float Rate,int Periods);
float DoPVCalc(float FutVal,float Rate,int Period, float Payment);
class Mortgage //declaring class Mortgage
{
public:
Mortgage(float BegBal, float Rate, int Periods); //constructor
~Mortgage(); // destructor
float GetBegBal(); // accessors get and set
void SetBegBal(float BegBal);
float GetRate();
void SetRate(float Rate);
int GetPeriods();
void SetPeriods (int Periods);
float GetPayment();
private:
float MBegBal, MRate, MPayment;
int MPeriods;
};
// constructor of Mortgage
Mortgage::Mortgage(float BegBal, float Rate, int Periods)
{
MPeriods = Periods;
MRate= Rate;
MBegBal = BegBal;
MPayment = CalcPayment(BegBal,Rate,Periods);
}
Mortgage::~Mortgage() //destructor
{
}
float Mortgage::GetBegBal()
{
return MBegBal;
}
void Mortgage::SetBegBal(float BegBal)
{
MBegBal = BegBal;
MPayment=CalcPayment(MBegBal,MRate,MPeriods);
}
float Mortgage::GetRate()
{
return MRate;
}
void Mortgage::SetRate(float Rate)
{
MRate = Rate;
MPayment=CalcPayment(MBegBal,MRate,MPeriods);
}
int Mortgage::GetPeriods()
{
return MPeriods;
}
void Mortgage::SetPeriods(int Periods)
{
MPeriods = Periods;
MPayment=CalcPayment(MBegBal,MRate,MPeriods);
}
// Get Payment
float Mortgage::GetPayment()
{
return MPayment;
}
// Calculate payment amount
float CalcPayment(float BegBal,float Rate,int Periods)
{
float Payment=0;
Payment=0-(BegBal*pow((1+Rate),Periods));
Payment=Payment/((pow((1+Rate),Periods)-1)/Rate);
return Payment;
}
void DisplaySchedule(float BegBal,float Rate,int Periods,float Payment)
{
float Interest,Principle,EndBal;
int BegPer=0,EndPer,CalcPer;
while (true)
{
cout << "Beginning Period : ";
cin >> BegPer;
if(BegPer > 0)
break;
cout << "Invalid Period.\n";
}
cout << "Ending Period : ";
cin >> EndPer;
CalcPer=Periods - (BegPer-1);
BegBal=DoPVCalc(0,Rate,CalcPer,Payment);
cout << "Period------BegBal------Payment-----Interest----Principle---EndBal------\n";
for (int Per=BegPer;Per<=EndPer;Per++)
{
Interest=BegBal*Rate;
Principle=Payment-Interest;
EndBal=BegBal-Principle;
cout << setw (10) << left << Per;
cout << setw (12) << right << setprecision(8) << BegBal;
cout << setw (12) << right << Payment;
cout << setw (12) << right << Interest;
cout << setw (12) << right << Principle;
cout << setw (12) << EndBal << "\n";
BegBal=EndBal;
}
}
float DoPVCalc(float FutVal,float Rate,int Period,float Payment)
{
float PresVal=0;
for (int i=1;i<Period+1;i++)
{
PresVal=PresVal+Payment/pow((1+Rate),i);
}
PresVal=PresVal+FutVal/pow((1+Rate),Period);
return PresVal;
}
int main()
{
GBegBal=-100000;
GRate=.01;
GPeriods=360;
Mortgage MyMort(GBegBal,GRate,GPeriods);
MyMort.SetBegBal(GBegBal);
MyMort.SetRate(GRate);
MyMort.SetPeriods(GPeriods);
float BegBal=MyMort.GetBegBal();
float Rate=MyMort.GetRate();
int Periods=MyMort.GetPeriods();
float Payment=MyMort.GetPayment();
void DisplaySchedule(float BegBal,float Rate,int Periods,float Payment);
return 0;
}
Help with MortgageProgram w/ Classes
Page 1 of 1
3 Replies - 2166 Views - Last Post: 25 October 2005 - 01:55 PM
#1
Help with Mortgage
Posted 24 October 2005 - 09:32 PM
Hi, this is my first post here. I'm majorly stuck on this program where I have to write a mortgage program using classes, and print out an ammortization table. This is what I have so far, and it compiles, but it won't print output. Please let me know if you have any suggestions. Thanks!
Replies To: Help with Mortgage
#2
Re: Help with Mortgage
Posted 25 October 2005 - 02:07 AM
Ok, I'm new here, and probably wrong. But don't you need an
"int main" in there somewhere?
"int main" in there somewhere?
#3
Re: Help with Mortgage
Posted 25 October 2005 - 05:28 AM
Actually, there is a main function (albeit difficult to spot).
The reason it has not dislayed the information is because of a slight syntax error in calling the display function. Change
to
It should then prompt the user for the start and end periods, and display the results.
The reason it has not dislayed the information is because of a slight syntax error in calling the display function. Change
void DisplaySchedule(float BegBal,float Rate,int Periods,float Payment);
to
DisplaySchedule(BegBal,Rate,Periods,Payment);
It should then prompt the user for the start and end periods, and display the results.
#4
Re: Help with Mortgage
Posted 25 October 2005 - 01:55 PM
Thanks so much. It runs perfectly now. I had to change the computation of rate also, but that was easy.
This post has been edited by Sara161616: 25 October 2005 - 02:04 PM
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|