Jump to content

Void function help please

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
hoku2000_99

hoku2000_99

    Newbie

  • Members
  • PipPip
  • 24 posts
I have to modify this program so that it uses a void function, rather than a value-returning function, to calculate the monthly payments. I started where I have my function prototype, but not too sure what to change or where to go next in this program.


#include <iostream>

#include <iomanip>

#include <cmath>


using std::cout;

using std::cin;

using std::endl;

using std::fixed;

using std::setprecision;


//fucntion prototype

void calcPayment (double,double, int);(what I changed)

//double calcPayment (double, double, int);(what I originally had)


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

	cout << fixed << setprecision(2) << endl;

	cout << "Credit union payment: $" << creditPayment << endl;

	cout << "Dealer payment: $" << dealerPayment << endl;


	return 0;

}//end of main function


//*****function definitions*****

void calcPayment (double prin, double monthRate, int months) (what I changed)


//double calcPayment(double prin, double monthRate, int months) (what I originally had)

{

	//calculates and returns a monthly payment

	double monthPay=0.0;

	monthPay=prin*monthRate/(1-pow(monthRate + 1, -months));

	return monthPay;

}//end of calcPayment function




#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Add a parameter to your function so it accepts the output variable as a reference to the original variable. You will need to use a reference or a pointer in your function to pass back the result.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog