Tuesday, July 14, 2009

Help with my C++ program please!!?

this is the error i am getting and don't how to fix it: cannot convert parameter 3 from 'const double' to 'double %26amp;'


this is my code:


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::setprecision;


using std::fixed;





//function prototypes


void getInput(double %26amp;);


void calcBonus(double, double, double %26amp;);


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 %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);





return 0;


} //end of main function





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


void getInput(double %26amp;sales)


{


//enter input items


cout%26lt;%26lt;"Enter sales: ";


cin%26gt;%26gt;sales;


}//end of getInput function

Help with my C++ program please!!?
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%26amp;", because double is small enough, that it doesn't make sense to pass by reference.
Reply:i don't understand your code what u want to do please write it properly.
Reply: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.

surveys

No comments:

Post a Comment