Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help.. Calculation problems

Status
Not open for further replies.

whatever1

Programmer
Nov 4, 2003
2
CA
Hello,

I really need some help on this program (load calculation) I have been working on but unfortunately my calculations don't work out correctly. I don't where am I going wrong. Code is on the bottom.

I enter the following data. Amount of load = 1500.00, Annual percentage rate = 14.0, and How many months to pay back the loan = 12.

The first line should output Payment Number = 1, Interest Paid = 17.50, Principal Paid = 117.18, Cumulative Interest = 17.50, Total Paid to Date = 134.68, New Balance Due = 1382.82. And the last line should be Payment Number = 12, Interest Paid = 1.55, Principal Paid = 133.13, Cumulative Interest = 116.17, Total Paid to Date = 1616.17, New Balance Due = 0.00.

Thank you for your help.

Yuna

Code:

#include <iostream> //for cout and cin
#include <conio.h> //for getch
#include <cmath>
#include <iomanip>

using namespace std;

void Getdata(double& Amount, double& Percent, int& Months);

void Caldata(double& Amount, double& Percent, int& Months,
double& InterestPaid, double& PrincipalPaid, double& CumlInterest, double& TotalPaidDate, double& MonthlyPayment, double& NewBalance, int& PaymentNum);

void Displaydata(double& Amount, double& Percent, int& Months, double& InterestPaid, double& PrincipalPaid, double& CumlInterest, double& TotalPaidDate, double& MonthlyPayment, double& NewBalance, int& PaymentNum);

void Displayheader(double& Amount, double& Percent, int& Months, double& MonthlyPayment);

void setvar(double& CumlInterest, double& TotalPaidDate, double& InterestPaid, int& PaymentNum, double& NewBalance, double& Amount);

void GetResponse(char& response);

int main()
{
double Amount;
double Percent;
double InterestPaid;
double PrincipalPaid;
double CumlInterest;
double TotalPaidDate;
double MonthlyPayment;
double NewBalance;
int PaymentNum;
int Months;
char response;

Getdata(Amount, Percent, Months);


Displayheader(Amount, Percent, Months, MonthlyPayment);

setvar(CumlInterest, TotalPaidDate, InterestPaid, PaymentNum, NewBalance, Amount);


do
{

Caldata(Amount, Percent, Months, InterestPaid, PrincipalPaid, CumlInterest, TotalPaidDate, MonthlyPayment, NewBalance, PaymentNum);

Displaydata(Amount, Percent, Months, InterestPaid, PrincipalPaid,
CumlInterest, TotalPaidDate, MonthlyPayment, NewBalance,
PaymentNum);
}while (PaymentNum <= Months);

GetResponse( response);

getch();

return 0;
}

void Getdata(double& Amount, double& Percent, int& Months)
{

cout << &quot;What is the amount of the loan? &quot;;
cin >> Amount;

cout << &quot;What is the annual percentage rate? &quot;;
cin >> Percent;

cout << &quot;How many months will you take to pay back the loan? &quot;;
cin >> Months;

while ((Percent < 1.0) || (Percent > 20.0))

{
cout << Percent << &quot;%&quot; << &quot; is not a vaild interest rate.\n&quot;;
cout << &quot;Try again. Please enter interest rate between 1% and 20% &quot;;
cin >> Percent;

}

cout << endl;

}

void Caldata(double& Amount, double& Percent, int& Months,
double& InterestPaid, double& PrincipalPaid,
double& CumlInterest, double& TotalPaidDate,
double& MonthlyPayment, double& NewBalance, int& PaymentNum)

{

MonthlyPayment = Amount * (Percent/1200)/(1.0 - pow((1.0 + (Percent/1200)), -Months));
InterestPaid = ((Percent/100) / Months) * Amount;
CumlInterest = CumlInterest + InterestPaid;
PrincipalPaid = MonthlyPayment - InterestPaid;
TotalPaidDate = TotalPaidDate + MonthlyPayment;
NewBalance = NewBalance - PrincipalPaid ;
PaymentNum++;
Months--;

}


void Displayheader(double& Amount, double& Percent, int& Months, double& MonthlyPayment)

{
MonthlyPayment = Amount * (Percent/1200)/(1.0 - pow((1.0 + (Percent/1200)), -Months));

cout << fixed << showpoint;

cout << setw(55) << &quot;Loan Amortization&quot; << endl << endl;


cout << setw(15) << &quot;Amount&quot; << setw(25) << &quot;Annual % Interest&quot; << setw(15) << &quot;Months&quot; << setw(25) << &quot;Monthly Payment&quot; << endl;

cout << setprecision(2) << setw(15) << Amount << setw(18) << setprecision(2) << Percent << setw(20) << Months << setw(27) << setprecision(2) << MonthlyPayment << endl;

cout << endl << endl;

cout << &quot;Payment&quot; << setw(12) << &quot;Interest&quot; << setw(16) << &quot;Principal&quot; << setw(20) << &quot;Cumlative&quot; << setw(20) << &quot;Total Paid&quot; << setw(20) << &quot;New Balance&quot; << endl;
cout << &quot;Number&quot; << setw(11) << &quot;Paid&quot; << setw(15) << &quot;Paid&quot; << setw(23) << &quot;Interest&quot; << setw(18) << &quot;to Date&quot; << setw(18) << &quot;Due&quot; << endl << endl;
cout << &quot;--------------------------------------------------------------------------------------------------&quot; << endl;

}

void Displaydata(double& Amount, double& Percent, int& Months, double& InterestPaid, double& PrincipalPaid,
double& CumlInterest, double& TotalPaidDate,
double& MonthlyPayment, double& NewBalance, int& PaymentNum)

{
cout << fixed << showpoint;


cout << setw(3) << PaymentNum << setw(14) << InterestPaid << setw(17) << PrincipalPaid << setw(20) << CumlInterest << setw(20) << TotalPaidDate << setw(20) << NewBalance << endl;

}

void setvar(double& CumlInterest, double& TotalPaidDate, double& InterestPaid, int& PaymentNum, double& NewBalance, double& Amount)

{
CumlInterest = 0.0;
TotalPaidDate = 0.0;
InterestPaid = 0.0;
PaymentNum = 0;
NewBalance = Amount;
}


//do
// {
void GetResponse(char& response)
{

cout << &quot;Do you want to continue? Y or N: &quot; << endl;
cin >> response;
cout << endl << endl;


while (response != 'Y' && response != 'y' && response != 'N' && response != 'n')
{
cout << &quot;You did not enter valid response. Please enter Y or N&quot;;
cin >> response;
}
}
//}while(response == 'Y' || response == 'y');
 
analyze the code with debugger.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Step through it using your debugger, it isn't very helpful to just say theres a problem in my code, and then dump it all here for someone else to have to read through.

As a general rule, you will get help if you at least try to show that you have looked for the anser yourself, and not just come straight here as soon as things get tricky. (and you will get more response if you just post relevant sections of code, not whole programs !)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top