Link to home
Start Free TrialLog in
Avatar of mustish1
mustish1

asked on

Function

Hi guys: This is a program with one return function name getPayment.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace test;
double getPayment(int, double, int);
void displayPayment(void);
int main()
{
      int carPrice = 0;
      int rebate = 0;
      double creditRate = 0.0;
      double dealerRate = 0.0;
      int term = 0;
      double creditPayment = 0.0;
      double dealerPayment = 0.0;

      cout << "Car price (after any trade-in):";
      cin >> carPrice;
      cout << "Rebate:";
      cin >> rebate;
      cout << "Credit union rate:";
      cin >> creditRate;
      cout << "Dealer rate:";
      cin >> dealerRate;
      cout << "Term in years:";
      cin >> term;
      displayPayment();
      creditPayment = getPayment(carPrice - rebate, creditRate / 12, term * 12);
      dealerPayment = getPayment(carPrice, dealerRate / 12, term * 12);
      cout << fixed << setprecision(2) << endl;
      cout << "Credit union payment: " << creditPayment << endl;
      cout << "Dealer payment: $" << dealerPayment << endl;
      system("pause");
      return 0;
}

double getPayment(int prin, double monthRate, int months)
{
      double monthPay = 0.0;
      monthPay = prin * monthRate / (1 - pow(monthRate + 1, -months));
      return monthPay;
}

void displayPayment(void)
{
      int monthlyPayment = 0;
      cout << "Enter monthly payment-->";
      cin >> monthlyPayment;
      cout << "Monthly Payment-->" << monthlyPayment << endl;
}
----------------------------------------------------------------
I change the getPayment function with void calcPayment(void) function but did mistake in the calculation of creditpayment and dealer payment. Can any one please help me out ? Thanks.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace test;
void CalcPayment(void);
void displayPayment(void);
int main()
{
      CalcPayment();
      system("pause");
      return 0;
}

void CalcPayment(void)
{
      double creditPayment = 0.0;
      double dealerPayment = 0.0;
      double monthPay = 0.0;
      int carPrice = 0;
      int rebate = 0;
      double creditRate = 0.0;
      double dealerRate = 0.0;
      int term = 0;
      int prin = 0;
      double monthRate = 0.0;
      int months = 0;
      cout << "Car price (after any trade-in):";
      cin >> carPrice;
      cout << "Rebate:";
      cin >> rebate;
      cout << "Credit union rate:";
      cin >> creditRate;
      cout << "Dealer rate:";
      cin >> dealerRate;
      cout << "Term in years:";
      cin >> term;
      displayPayment();
      //creditPayment = carPrice - rebate, creditRate / 12, term * 12;
      //dealerPayment = carPrice, dealerRate / 12, term * 12;
      monthRate = creditRate / 12;
      months = term * 12;
      creditPayment = (carPrice - rebate) * monthRate / (1 - pow(monthRate + 1, -months));
      dealerPayment = carPrice * monthRate / (1 - pow(monthRate + 1, -months));
      cout << fixed << setprecision(2) << endl;
      cout << "Credit union payment: " << creditPayment << endl;
      cout << "Dealer payment: $" << dealerPayment << endl;

}


void displayPayment()
{
      int monthlyPayment = 0;
      cout << "Enter monthly payment-->";
      cin >> monthlyPayment;
      cout << "Monthly Payments-->" << monthlyPayment << endl;
}
Avatar of crysallus
crysallus
Flag of Australia image

It looks like you're using creditRate in both calculations, rather than using dealerRate in the dealerPayment calculation. Is that the problem?

Also, if you're not aware, you might want to be careful about your divide by 12, as that's an integer division which will only yield an integer result i.e. 18 / 12 = 1, not 1.5. In order to not have the decimal part truncated, use creditRate / 12.0.
Avatar of mustish1
mustish1

ASKER

>It looks like you're using creditRate in both calculations, rather than using dealerRate in the dealerPayment calculation. Is that the problem?

Yes I mix it up the calculation.
Can you please help me in that line

      dealerPayment = carPrice * monthRate / (1 - pow(monthRate + 1, -months));

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of crysallus
crysallus
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot.