Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Fahrenheit-Celsius Conversion Program in C

  • 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, Registered Users 2 Posts: 23,202 ✭✭✭✭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, Registered Users 2 Posts: 1,454 ✭✭✭Smoggy


    *sniff* smells like homework


  • Moderators, Music Moderators Posts: 25,872 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, Registered Users 2 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, Registered Users 2 Posts: 23,202 ✭✭✭✭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, Registered Users 2 Posts: 26,449 ✭✭✭✭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, Registered Users 2 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