Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Fahrenheit-Celsius Conversion Program in C

Options
  • 25-09-2007 11:58am
    #1
    Closed Accounts Posts: 388 ✭✭


    I am trying to write a program in C for class that converts Fahrenheit to Celsius and created this:

    [PHP]#include <stdio.h>
    #define GIVEN_ONE 9/5
    #define GIVEN_TWO 32

    double main()

    {
    double fahr, cels;

    /* Entering temperature in Fahrenheit */
    printf("Enter the temperature in Fahrenheit: ");
    scanf("%lf", &fahr);

    /* Formula for converting Fahrenheit to Celsius */
    fahr = cels * GIVEN_ONE + GIVEN_TWO;

    /* Give temperature in Celsius */
    printf("The temperature is %f in Celsius. \n", cels);

    return(0);
    } [/PHP]

    I was hoping it would work but the output in Celsius just comes out to 0.00000. Anyone with knowledge in C that can help?


Comments

  • Registered Users Posts: 23,212 ✭✭✭✭Tom Dunne


    I am trying to write a program in C for class that converts Fahrenheit to Celsius and created this:

    [php]#include <stdio.h>
    #define GIVEN_ONE 9/5
    #define GIVEN_TWO 32

    double main()

    {
    double fahr, cels;

    /* Entering temperature in Fahrenheit */
    printf("Enter the temperature in Fahrenheit: ");
    scanf("%lf", &fahr);

    /* Formula for converting Fahrenheit to Celsius */
    fahr = cels * GIVEN_ONE + GIVEN_TWO;

    /* Give temperature in Celsius */
    printf("The temperature is %f in Celsius. \n", cels);

    return(0);
    } [/php]
    I was hoping it would work but the output in Celsius just comes out to 0.00000. Anyone with knowledge in C that can help?

    You are reading in fahr, then you are setting fahr to (cels * given_one + given_two). As cels is undefined, it defaults to zero. Therefore zero * anything=0.


  • Registered Users Posts: 1,466 ✭✭✭Smoggy


    *sniff* smells like homework


  • Moderators, Music Moderators Posts: 25,868 Mod ✭✭✭✭Doctor DooM


    fahr = cels * GIVEN_ONE + GIVEN_TWO;

    is the prob, think you need to swap them, cels is currently = 0.


    Edit Oops, sorry, didnt see the answer there.

    The OP actually says its for class!


  • Registered Users Posts: 7,541 ✭✭✭irlrobins


    you don't say...;)

    looks like UCD Eng stuff to me.... But as Tom says you have fahr = cels instead of cels =fahr.


  • Closed Accounts Posts: 5,029 ✭✭✭um7y1h83ge06nx


    Looks like the lads have solved your problem but just have to reply to say that this thread has got me all nostalgic about being in college! :p

    Hope you enjoy college and programming, they're hard to beat! :D


  • Advertisement
  • Registered Users Posts: 23,212 ✭✭✭✭Tom Dunne


    SDooM wrote:
    fahr = cels * GIVEN_ONE + GIVEN_TWO;

    is the prob, think you need to swap them, cels is currently = 0.

    I wasn't actually going to give him the full answer. ;)


  • Closed Accounts Posts: 583 ✭✭✭monkey tennis


    Think I remember this one from K&R...

    Now that your problem has been solved, why are you telling main() to return a double? :confused:


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    Smoggy wrote:
    *sniff* smells like homework
    doesn't matter if it is or not :p, at least s/he made an attempt at it, so when other posters point out the mistakes s/he will learn more from it than just coming on here and asking:

    "i need a temperature convertor, write me one now!!!1"

    not everyone is a perfect programmer, and it often takes another set of eyes to figure out mistakes.

    and to be totally pedantic about it, s/he says it's for class :p


  • Registered Users Posts: 760 ✭✭✭mach1982


    One tip all C programs should start with int main()


  • Closed Accounts Posts: 583 ✭✭✭monkey tennis


    mach1982 wrote:
    One tip all C programs should start with int main()

    IIRC, the explicit 'int' isn't necessary, as all functions are assumed to return an int by default in C.


  • Advertisement
Advertisement