Thread: Returning a float?

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    3

    Question 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;
    }
    Last edited by mandalord; 03-26-2007 at 03:20 PM.

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    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.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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;

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    3

    Thanks

    Lol thanks I was being an idiot. Hmm double, cool thanks Daved I'll use that instead.

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    If you want to stay anonymous, I would change the second line in your code header comment

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    3

    oops

    Being an idiot again =/

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    using namespace std; //No idea but it looks cool =/
    LOL @ your comment.

    A namespace is just a way to package various things. The std namespace contains things like cout and cin.

    For example, if you didn't include that line or a similar one, you would have to always clarify to the compiler where to find cout and cin.... like this:

    Code:
    std::cout << "Example" << std::endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM