Friday, July 31, 2009

I need an actual example of how to write a 'float function' in Visual Studio 2005 for C++!!!!!!!!!!!!!!!!!!!!!

i have the program Microsoft Visual Studio 2005





i am using this in my computer science class





i know how to properly write a 'string function' in C++ but in my homework assignment we need to write a 'string function' and a 'float function' and i have no idea what im doing wrong





i do exactly the same thing i do w/ the 'string function' to the 'float function' but it wont compile and i dont understand what the error is trying to tell me to fix, please god someone help me!!

I need an actual example of how to write a 'float function' in Visual Studio 2005 for C++!!!!!!!!!!!!!!!!!!!!!
When you ask a question like this in the future, it might help to also post the error that you get when compiling your code. It actually can tell you a lot!





Based on the code you posted, I see a couple of problems:





1) You function signature: float findarea (float h, float a, float b);


Doesn't match your function definition: float findarea (float h, float a, float b, float area)





That will be a problem. Looks like you can eliminate the 'float area' variable and they should match then. Remember that the signature you put at the beginning of your code needs to match the function you write later on!





2) when you call findarea: cout %26lt;%26lt; findarea;


You are not passing any of the variables! You need to send the variables to the function for it to do any work. You probably want: cout %26lt;%26lt; findarea(h, a, b);





3) Finally, in the function itself, you do work on the variable 'area' but you return an unitialized variable 'testarea'. You probably want:





float findarea (float h, float a, float b, float area)


{


return ((1/2)*(h))*(a+b);


}








If you still have problems, you probably should post a new question with the part of the code that matters and the error messages that you are seeing when you compile. But I think these three changes will compile OK for you (assuming you make the same kind of changes to your other functions). There is nothing special about float functions, you just need to brush up on the rules for writing functions in general.





Good Luck!
Reply:Can you post your code? Do you just need a function that handles floating point numbers?





float division(float x, float y){


return x/y;


}





int main(){


cout%26lt;%26lt;division(10,3)%26lt;%26lt;endl;


}
Reply:Edit your question and put in the code you are trying to use...we can't really help you with your errors unless we see the code. This also allows people to see that you did give it some effort and you need help rather than looking for homework answers.


No comments:

Post a Comment