Question:
help with my C++ program please!!?
I can do it!
2008-04-08 12:32:31 UTC
this is the error i am getting and don't how to fix it: cannot convert parameter 3 from 'const double' to 'double &'
this is my code:
#include
#include

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

//function prototypes
void getInput(double &);
void calcBonus(double, double, double &);
void displayBonus(double);

int main()
{
//declare constant and variables
const double RATE = .1;
double sales = 0.0;
double bonus = 0.0;

//call funtions to get input
//call functions to calculate and display bonus amount
getInput(sales);
calcBonus(sales, bonus, RATE);
displayBonus(bonus);

//display output item
cout << fixed << setprecision(2);

return 0;
} //end of main function

//*****function definitions*****
void getInput(double &sales)
{
//enter input items
cout<<"Enter sales: ";
cin>>sales;
}//end of getInput function
Four answers:
Michael Safyan
2008-04-08 15:59:34 UTC
The problem is that your declaration of and the definition for "calcBonus" do not match up. Your declaration indicates that the third parameter is a non-const reference, yet you pass in a const. You should change the declaration of "calcBonus" to match that of your definition, and that should fix everything.



ASIDE:

It is common in C to use "output parameters" (i.e., parameters which are reference types) for reporting results. However, it is generally preferred among C/C++ programmers for parameters to be "input", only, and for the results to be given through the function's return type.



That is, it would be preferrable to use:



inline double calcBonus(double sales, double RATE)

{

return sales * RATE;

}



Note that I also declared the function "inline", because it would be a waste to create a new stack frame for such a simple multiplication operation. Also, I used "double" instead of "const double&", because double is small enough, that it doesn't make sense to pass by reference.
heb3
2008-04-08 15:52:36 UTC
The only function you have that takes at least 3 parameters is calcBonus(). And you are passing a const as the 3rd parameter but it wants a reference. If RATE was an ordinary double, this would work. However, a const is much like a hard-code number (0.1). And passing a reference without making it a const reference implies that the function has the ability to *change* that value. But you cannot change 0.1 to have a value of anything except 0.1. (Unless you are using an ancient version of Fortran!).



Easiest fix to this is to change the function declaration so that you just pass a double as the 3rd parameter and not a reference to a double.
tonya
2016-05-26 05:49:10 UTC
Not anymore, at one time C++ was just an extension of C. Since then, C has changed, and so has C++. However that being said it's not unusual for the same program to compile both C and C++. The method is the same, but there are just some differences int he libraries that you need the compiler to link to.
priyani
2008-04-08 12:44:36 UTC
i don't understand your code what u want to do please write it properly.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...