Function help

This is a discussion on Function help within the C++ Programming forums, part of the General Programming Boards category; My output for the variable celsius is 0 everytime. I can't figure it out =(. Can anyone spot what is ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    Function help

    My output for the variable celsius is 0 everytime. I can't figure it out =(. Can anyone spot what is wrong?

    Code:
    #include <iostream>
    using namespace std;
    
    double get_celsius(double);
    
    int main()
    {
    	double fahrenheit, celsius;
    
    	cout << "This progam converts fahrenheit to celsius.\nPlease input a fahrenheit temperature...\n";
    		cin >> fahrenheit;
    
    	celsius = get_celsius(fahrenheit);
    
    	cout << fahrenheit << " fahrenheit is " << celsius << " celsius.\n"; 
    	return 0;
    }
    
    double get_celsius(double fahrenheit)
    {
    	return (5/9)*(fahrenheit-32);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    Hint: with integer division, 5/9=0
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Ok, i just put .5555 instead of 5/9, how would you code 5/9 to work?

    Code:
    double get_celsius(double fahrenheit)
    {
       double x = 5/9,
                   result;
      
        result = (fahrenheit - 32) * x;
        return result;
    would that work?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    how would you code 5/9 to work?
    An easy way is to write 5.0/9.0 instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,934
    Hint:
    5 == treated as an integer
    5.0 == treated as a double
    5.0f == treated as a float

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21