Converting fahrenheit to celsius but getting 0.00 output

This is a discussion on Converting fahrenheit to celsius but getting 0.00 output within the C Programming forums, part of the General Programming Boards category; For some reason when I input a number in, it comes out 0.00. What did I do wrong? Do I ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    36

    Converting fahrenheit to celsius but getting 0.00 output

    For some reason when I input a number in, it comes out 0.00. What did I do wrong? Do I need to int Fahrenheit?

    Code:
    /* Directives */
    
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    
    {
    
    float fahrenheit, T;
    
    printf("Enter a temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);
    T = (5/9) * (fahrenheit - 32);
    printf("Celsius is %.2f \n", T);
    
    /* End Program */
    return(0);
    
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Code:
    T = (5/9) * (fahrenheit - 32);
    In the red bit you are doing integer maths so the result is 0. As you cant have integer units between 1 and 0 so it rounds down.

    Try:
    Code:
    T = (5.0f/9.0f) * (fahrenheit - 32.0f);

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Although, you only need one of them to be a float to get a proper result.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Ah, no wonder. Thanks so much.

    How would I define m/s to ft/s as a constant?

    I'm supposed to use 4 significant digits for your conversion factor between m/s to ft/s (which should be defined as a
    constant). How would I do this?

    My program is outputting 123.45 m/s, but I also need it to say next to the 123.45 m/s, or 405.09 ft/s.

    ex: Speed is: 123.45 m/s, or 405.09 ft/s.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How do you convert m/s to ft/s?

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    To go from meters to feet, times meter by 3.28084. But I was wondering if there was a way to make this a constant cause the hw prompt says it should be a constant. Like #define meter to feet 3.28084?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want. Macro names can't have spaces in them, though.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Ok, so I just put in #define tofeet 3.28084 for the constant and use the word tofeet in my equation.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Thanks for the help!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  2. why this output?
    By dredre in forum C Programming
    Replies: 4
    Last Post: 05-08-2004, 04:09 PM
  3. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 05:02 PM
  4. Control different DA output value!
    By Hunterhunter in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-13-2003, 11:11 AM
  5. Profiler for g++
    By SilentStrike in forum Linux Programming
    Replies: 5
    Last Post: 04-29-2002, 11:01 PM

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