CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Posts
    6

    Function always returns 0

    I have an issue with a program I am writing for an assignment, I have been trying to resolve it for the past 2 days. My function keeps returning 0 and I dont know why.

    This is my function prototype

    Code:
    float farToCel ( int ); //Farhenheit to Celcius function prototype using pass by value
    This code is from main where I call the function

    Code:
    case 2:
    	cout << "Enter the value in Farenheit ";
    	cin >> value;
    	a = farToCel(value);
    	cout << value << " Farenheit is equal to " << a << " Celcius\n" << endl;
    	break;
    And, here is the function definition

    Code:
    float farToCel(int value)
    {
    	float result;
    
    	result = ((5 / 9) * (value - 32));
    	return result;
    }
    I have even tried debugging and value is being passed to the function correctly and it still returns a 0 every time. Could someone please let me know what I'm doing wrong here?

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Function always returns 0

    5/9 is an integer division. How many 9's in 5, answer 0. 0 * any_num = 0, therefore 0 always gets returned.

    try 5.0/9.0
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  3. #3
    Join Date
    Mar 2009
    Posts
    6

    Re: Function always returns 0

    I wanna kick myself right now, I've been struggling with this for two days and all I needed was a .0 at the end.

    Thank you very much Russco

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured