issue with void function

Write your question here.
Hello, I am new to coding and am having an issue with the void function in this code. I am getting an error for the calcPayment function that states: [Error] void value not ignored as it ought to be and another error stating [Error] ambiguating new declaration of 'double calcPaymet(int, double, int, double &)
Can somebody please explain what is wrong with the code?
Thanks!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

//function prototypes
void calcPayment(int, double, int, double &);
void displayPayment(double);

int main()
{
	//declare variables
	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;
	double monthPay = 0.0;
	
	//get input items
	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;
	
	//call function to calculate payments
	creditPayment = calcPayment(carPrice - rebate, creditRate / 12, term *12, monthPay);
	dealerPayment = calcPayment (carPrice, dealerRate / 12, term * 12, monthPay);
	
	//Display payments
	cout << fixed << setprecision(2) << endl;
	cout << "Credit union payment: $";
	displayPayment(creditPayment);
	cout << endl;
	cout << "Dealer payment: $";
	displayPayment(dealerPayment);
	cout << endl;
	
	system("pause");
	return 0;
}// end of main function

//**function definitions
double calcPayment(int prin, double monthRate, int months, double &mthlyPay)
{
	//calculates and returns a monthly Payment
	mthlyPay = prin * monthRate / (1-pow(monthRate + 1, -months));
	return mthlyPay;
}// end of getPayment function
void displayPayment(double mthlyPay)
{
	cout << mthlyPay;
}// end of displayPayment function 
Last edited on
Hi there

I will try to help best to my abilities :) there might be mistakes on my part :D

so this is your function prototype

void calcPayment(int, double, int, double &);

what you say is that I will send integer, double, another integer and refernce to double.

This is your line from inside main() invoking function:

creditPayment = calcPayment(carPrice - rebate, creditRate / 12, term *12, monthPay);

and this is your function
double calcPayment(int prin, double monthRate, int months, double &mthlyPay)



What I think you should do here is :

prototype:
void calcPayment(int, int, double, int, double);

function call inside main()
I removed creditPayment variable from the front of the function call.

calcPayment(carPrice, rebate, creditRate, term, monthPay);

then function itself, in your prototype you state that function returns void
but you want to return double

double calcPayment(int prin, double monthRate, int months, double &mthlyPay)
in bold double means that you will return variable of type double back to where function was called (in your case to the main)

so it should be something like this (copy prototype and add to each type name of the variable

void calcPayment(int carPrice_, int rebate_, double creditRate_, int term , double monthPay_)

i have used _ to change name, but you can use something different as far as I know :)
then inside your function you have to do all correct callculations, remember that
all variables will come in order you have put them, so first is carPrice then its rebate and last one will be monthPay.

I hope i didn't make to many mistakes :D if I did I am sorry


Last edited on
Thank you for your response. I have question regarding the creditPayment variable being removed. Where does it go afterwards because I need to display the payment for the creditPayment and the dealerPayment. Also, when calculating the int prin variable, the value differs for the creditPayment and dealerPayment, so those need to stay. For the creditPayment int prin equals carPrice - rebate, whereas for the dealerPayment int prin equals carPrice alone.


Here is the code with the changes that you recommended--I may have misinterpreted some, so I am sorry if I did.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

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

int main()
{
	//declare variables
	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;
	double monthPay = 0.0;
	
	//get input items
	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;
	
	//call function to calculate payments
	creditPayment = calcPayment(carPrice, rebate, creditRate, term, monthPay);
	dealerPayment = calcPayment (carPrice, rebate, creditRate, term, monthPay);
	
	//Display payments
	cout << fixed << setprecision(2) << endl;
	cout << "Credit union payment: $";
	displayPayment(creditPayment);
	cout << endl;
	cout << "Dealer payment: $";
	displayPayment(dealerPayment);
	cout << endl;
	
	system("pause");
	return 0;
}// end of main function

//**function definitions
void calcPayment(int carPrice, int rebate, double creditRate, int term, double monthPay)
{
	//declare variables	
	int prin = carPrice - rebate;
	double monthRate = creditRate / 12;
	int months = term * 12;
	
	//calculates and returns a monthly Payment
	monthPay = prin * monthRate / (1-pow(monthRate + 1, -months));
}// end of getPayment function
void displayPayment(double mthlyPay)
{
	cout << mthlyPay;
}// end of displayPayment function  
Hi eRamirez97


I will answer your question best to my ability :)

think of a function like yours calcPayment or displayPayment like a black box.
What you do is you put ingredients to the box and function does its magic. User of your program does not need to know (nor should not know) what is happening inside function (not that this is some kind of secret anyway)

so in other word if you put two values lets say value a = 5 and value b = 10

inside function you can do number of things with this, multiply, divide, add, substruct or something else, but all of that is happening inside function.

Now in your original code you do this creditPayment = calcPayment(carPrice - rebate, creditRate / 12, term *12, monthPay);

this type of operations should be happening inside function not as a parameter that you are trying to pass it to the function.

I think you should get paper and work out how can i split my program into small logical parts, imagine company, where each person does everyjob there is, it would be a mess.
but businesses are divided into department, accounts deals with all accounting issues, HR with staff, R&D with new products etc. same your functions must work.

so you can have function that is CreditPayment, you will pass required data to that function and function will return value. For example as a result of calculation.

let me know if that helps, if I have more time I will try to analyse your code and provide more help. Or maybe someone with more experience will help as well :)
Hi eRamirez97

I am looking at the code, and i have few questions:

1. What is the difference between creditPayment and dealerPayment?
in your code you are calling both with same arguments:
creditPayment = calcPayment(carPrice, rebate, creditRate, term, monthPay);
dealerPayment = calcPayment (carPrice, rebate, creditRate, term, monthPay);

I am not sure if this is correct way of what you want to do... but first I would have to understand what is that you want to do?

2. I am assuming that you are aware of fact that when double and int mix you might lose precision? for example if you want to save multiplication of double and int and save it to int
double 3.99 * int 2 = int 7 not the correct answer double = 7.98.
Some of the variables could be double carPrice or rebate I think.
(int carPrice, int rebate, double creditRate, int term, double monthPay)

could you walk me trough how program should works?
Hello xxvms, Thank You for helping me out with the question. I figured out a way to make the code work by removing the displayPayment function and getting rid of the variables such as prin and monthlyPay from the function definition. I also set it up to display the payment within the function.
Below is the code that worked for me. Thank You

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cmath>11
#include <iomanip>
using namespace std;

//function prototypes
void calcPayment();

int main()
{
	//declare variables

	double creditPayment = 0.0;
	double dealerPayment = 0.0;
	
	//get input items
	
	
	//call function to calculate payments
	calcPayment();

	
	system("pause");
	return 0;
}// end of main function

//**function definitions
void calcPayment()
{
	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;
	double monthPay = 0.0;
	//declare variables	
	//double monthRate = creditRate / 12;
	//int months = term * 12;
	
	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;
		
	int months = term * 12;
	
	creditPayment = (carPrice-rebate) * (creditRate / 12) / (1-pow((creditRate / 12) + 1, -months));
	dealerPayment = carPrice * (dealerRate / 12) / (1-pow((dealerRate / 12) + 1, -months));
	
	cout << fixed << setprecision(2) << endl;
	cout << "Credit union payment: $" << creditPayment << endl;
	cout << "Dealer payment: $" << dealerPayment << endl;

}// end of getPayment function 
Also,
In response to your question, the difference between creditPayment and dealerPayment is that creditPayment has the (carPrice - rebate), and (creditRate / 12) for two portions of the equation whereas the dealerPayment has the carPrice alone and (dealerRate / 12) instead of (creditRate / 12).

Thank You very much for your assistance with the problem
Topic archived. No new replies allowed.