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;
}
mustish1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mustish1Author Commented:
Thanks a lot.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.