# Returning a float?

• 03-26-2007
mandalord
Returning a float?
I am making a calculator for 8th grade geometry class. We are learning about trigonometric ratios. My problem is, I got the whole program written, and then I realized that my sines and cosines should be getting decimals. I realize the problem is because I'm using the wrong variable type. I checked it, and I'm using floats. I think the problem is my funtion is returning ints. Criticism welcome as long as it has to do with my problem. I'm new at this programming thing, just started 25 minutes ago in fact, so it's gonna be sloppy.

Edit-----> Lol I just figured it out. I was using an int type function lol! I'm stupid!I'm stupid!I'm stupid!I'm stupid!

Source Code ------->
Code:

```// Trigonometric Ratios Helper.cpp : Defines the entry point for the console application. // By { A nanny Mouse! } #include "stdafx.h" #include <iostream> //Input Output using namespace std; //No idea but it looks cool =/ int sine ( float b, float c ); int main() {   float Leglength1;   float Leglength2;   float hypotenuse;   float sinea;   float cosa;   float tana;   float tanb;   cout<<"            {Blocked for forum use}'s  Amazing Trigonometric Ratio Helper!"<<"\n";   cout<<"                            B" <<"\n";   cout<<"                          /|" <<"\n";   cout<<"                          / |" <<"\n";   cout<<"                    Hyp /  |Leg2" <<"\n";   cout<<"                        /  |" <<"\n";   cout<<"                      A----C" <<"\n";   cout<<"                        Leg1" <<"\n";   cout<<"Please enter the length of a leg: ";   cin>> Leglength1;   cin.ignore();   cout<<"Please enter the length of the other leg: ";   cin>> Leglength2;   cin.ignore();   cout<<"Now the tricky part, enter the hypotenuse: ";   cin>> hypotenuse;   cin.ignore();   sinea = sine(Leglength2, hypotenuse);   cout<<"                            B" <<"\n";   cout<<"                          /|" <<"\n";   cout<<"                          / |" <<"\n";   cout<<"                      " << hypotenuse << "  /  |"<< Leglength2 <<"\n";   cout<<"                        /  |" <<"\n";   cout<<"                      A----C" <<"\n";   cout<<"                        "<< Leglength1 <<"    " << "\n";   cout<<"Sin A: "<< sinea <<"\n";   cin.get(); } int sine ( float b, float c ) {   return b /= c; }```
• 03-26-2007
Noir
Yeah, you should be returning a float instead of an int. The int is trimming off anything past the decimal point and you probably wanted those numbers. ;)
• 03-26-2007
Daved
Did you try changing the return type to float instead of int for your sine function (both in the prototype at the top and the definition at the bottom)?

You should actually use double instead of float, it is more accurate and is the default type for decimal numbers in C++.

Finally, b/= c means that b is now equal to b / c. You don't really need to save anything to b, so this should be good enough:
Code:

`return b / c;`
• 03-26-2007
mandalord
Thanks
Lol thanks I was being an idiot. :confused: Hmm double, cool thanks Daved I'll use that instead.
• 03-26-2007
KONI
If you want to stay anonymous, I would change the second line in your code header comment
• 03-26-2007
mandalord
oops
Being an idiot again =/
• 03-26-2007
MacGyver
Code:

`using namespace std; //No idea but it looks cool =/`
`std::cout << "Example" << std::endl;`