Sunday, July 12, 2009

Help with my C++ program please!!! i am getting this error that i don't know how to fix:?

cannot convert parameter 1 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;





//fucntion prototype


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


double calcAverage(double, double, double);





int main ()


{


//decalare variables


const double labAve = .50;


const double quizAve = .20;


const double examAve = .30;


double average = 0.0;


double getAverage = 0.0;





//enter functions to het input items


getInput(labAve, quizAve, examAve);


getAverage = calcAverage(labAve, quizAve, examAve);





//calculate and display average


cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);


cout %26lt;%26lt;"Your average is : " %26lt;%26lt; average %26lt;%26lt; endl;





return 0;


} //end of main function





//**program defined functions ****

Help with my C++ program please!!! i am getting this error that i don't know how to fix:?
#include%26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::setprecision;


using std::fixed;





//function prototype


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


double calcAverage(double, double, double);





int main ()


{


//decalare variables


double labAve; // %26lt;- Removed const as you are putting values


double quizAve; // %26lt;- into these variables


double examAve; // %26lt;- If you not changing value, use const


double average; // %26lt;- Do not need a value assigned as it is assigned a value from a function call.





//enter functions to get input items


getInput(labAve, quizAve, examAve);


// calculate average of input and assign result to average.


average = calcAverage(labAve, quizAve, examAve);





//calculate and display average


cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);


cout %26lt;%26lt;"Your average is : " %26lt;%26lt; average %26lt;%26lt; endl;





return 0;


} //end of main function





This should be what the code meant to do.





PS (To previous answerer). The joke was very funny.
Reply:%26amp; = address of


* = pointer to


double * ptr = %26amp;variable


you cannot define a function like


void function(double %26amp;ptr);


the correct way


void function(double *ptr);





learn C before you learn C++





one more thing remember to dereference your variables





cin %26gt;%26gt; *ptr;
Reply:No you can't. Use const when you can declare a variable at initialization and will not touch it otherwise. Yes I know there are supposed to be specialized cases where you can change it but DON'T. Take that filthy const out of the declaration of the variables YOU WANT TO INPUT. And while you are at it, since you send calcAverage into getAverage try printing it rather than average (which you do after having initialized it to zero and done nothing else).





Okay. I don't like const so I try to avoid it (#define is better) but I'll give you a hint. There is actually, in C++ a difference between passing the address of a variable and passing a pointer to that variable. I'm not saying you can't do it, I'm just saying the preprocessor can tell the difference between an address of something and a pointer to something.








Just remember:





" In C, you merely shoot yourself in the foot.





" In C++, you accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical care is impossible, because you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there." "





--old UseNet joke.


No comments:

Post a Comment