• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

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;
}
0
mustish1
Asked:
mustish1
  • 3
  • 2
1 Solution
 
crysallusCommented:
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.
0
 
mustish1Author Commented:
>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.
0
 
mustish1Author Commented:
Can you please help me in that line

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

Thanks.
0
 
crysallusCommented:
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 monthCreditRate = 0.0;
      double monthDealerRate = 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;
      monthCreditRate = creditRate / 12;
      monthDealerRate = dealerRate / 12;
      months = term * 12;
      creditPayment = (carPrice - rebate) * monthCreditRate / (1 - pow(monthCreditRate + 1, -months));
      dealerPayment = carPrice * monthDealerRate / (1 - pow(monthDealerRate + 1, -months));
      cout << fixed << setprecision(2) << endl;
      cout << "Credit union payment: " << creditPayment << endl;
      cout << "Dealer payment: $" << dealerPayment << endl;

}

Open in new window

And I was a little mistaken what I said about the integer division. creditRate is a double, so the division is not an integer division. It's only an integer division if both operands are integers.
0
 
mustish1Author Commented:
Thanks a lot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now