Need assistance with void function

void function is not displaying results properly

Page 1 of 1

1 Replies - 707 Views - Last Post: 21 January 2009 - 10:55 PM Rate Topic: -----

#1 greenbluekidz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 05-January 09

Need assistance with void function

Post icon  Posted 21 January 2009 - 04:22 PM

I am working on an exercise where I am supposed to create a function called displayPayment(). The function is supposed to be able to accept and monthly payment and then display the payment on the screen. I have spent hours on this and my query is not producing the correct results. Can someone take a look at the code that I have done and point out where my error lies? My instructor is in Chicago for a seminar and cannot be contacted for further assistance until next week. When I run the query, I am getting the following results:

Quote

Car Price : 16000
Rebate : 3000
Credit Union Rate : .08
Dealer Rate : .03
Term In Years : 4

Credit Union Payment : $ 0.08
Dealer Payment : $ 0.03
Monthly Payment: $00411163
Press any key to continue . . .


To test the code use the following:
Car Price - 16000
Rebate - 3000
Credit Union Rate - .08
Dealer Rate - .03
Term - 4

Thanks for any direction you can give me. I am trying very hard to absorb C++


#include <iostream>
#include <iomanip>
#include <cmath>

using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;

//function prototype
double calcPayment (double, double, int);
void displayPayment (double, double);

int main ()
{
 // declare variables
  double carPrice = 0.0;
  double rebate = 0.0;
  double creditRate = 0.0;
  double dealerRate = 0.0;
  int term = 0;
  double creditPayment = 0.0;
  double dealerPayment = 0.0;

// Get input items
  cout << "Car Price : ";
  cin >> carPrice;
  cout << "Rebate : ";
  cin >> rebate;
  cout << "Credit Union Rate : ";
  cin >> creditRate;
  cout << "Dealer Rate : ";
  cin >> dealerRate;
  cout << "Term In Years : ";
  cin >> term;

// Call function to calculate payments
  creditPayment = calcPayment (carPrice - rebate, creditRate / 12, term * 12);
  dealerPayment = calcPayment (carPrice, dealerRate / 12, term * 12);

// Display Payments
  displayPayment (creditRate, dealerRate);  
return 0;
// End of main function
}

// ***** Function Definitions *****
double calcPayment ( double prin, double monthRate, int months )
{
// Calculates and returns a month payment
double monthPay = 0.0;
monthPay = prin * monthRate / ( 1 - pow(monthRate + 1, -months));
return monthPay;
//end of calcPayment function
}
void displayPayment (double creditPayment, double dealerPayment)
{
cout << fixed << setprecision (2) << endl;
cout << "Credit Union Payment : $ " << creditPayment << endl;
cout << "Dealer Payment : $ " << dealerPayment << endl;
cout << "Monthly Payment: $" << calcPayment << endl;
}
// end of displayPayment function



This post has been edited by greenbluekidz: 21 January 2009 - 04:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need assistance with void function

#2 matthew180  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: Need assistance with void function

Posted 21 January 2009 - 10:55 PM

In your displayPayment function, the last cout line, if calcPayment is supposed to be a function call, then you are missing parameters. If it is supposed to be a variable, then you do not set it anywhere in the displayPayment function...

Also, you should not use floating point for money math.

Matthew
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1