Thread: How to use a user defined function to convert Fahrenheit to Celcius

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    1

    How to use a user defined function to convert Fahrenheit to Celcius

    Hello, I am very new to c++ programming and I a trying to create a program with a user defined function to convert Fahrenheit to Celcius. Here is what I have so far, but it does not run:

    Code:
     
    //this program converts Fahrenheit to Celcius
    #include<iostream>
    using namespace std;
    
    int main()
    {
        float Fahrenheit, Celcius;
    
        cout << "Enter the temperature in Fahrenheit: ";
        cin >> Fahrenheit;
        Celcius = (5.0/9.0)*(Fahrenheit - 32);
        cout<<"The temperature in Celcius: " <<Celcius<<endl;
       
        return 0;
    }
    When I run this, I get an error that Celcius was not declared in the scope, even though I thought I had declared it at the top. Additionally, I am not sure if I should be using float or double. Any help or guidance on this would be appreciated! Thank you.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Are you sure you didn't make any spelling errors before? I'm asking because the code you posted here compiles and runs just fine.

    About your second question, when in doubt, use double. It's a good default for real numbers. float is typically half the size but also half as accurate.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by hay_123 View Post
    I am not sure if I should be using float or double.
    Float has adequate range for any temperature you're likely to encounter in the real universe.

    You should also initialize your variables, in case the user provides bad input.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Guest
    Guest
    @OP: And the correct spelling is Celsius btw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-23-2013, 09:43 AM
  2. User-defined function
    By ulti-killer in forum C Programming
    Replies: 5
    Last Post: 06-10-2012, 05:27 AM
  3. User defined cos function
    By zfite in forum C Programming
    Replies: 11
    Last Post: 04-03-2011, 02:40 AM
  4. how to convert char string to user defined data type
    By prdeepss in forum C Programming
    Replies: 5
    Last Post: 08-02-2009, 09:59 AM
  5. Replies: 14
    Last Post: 03-02-2008, 01:27 PM

Tags for this Thread