Thread: Celsius-Fahreneit program

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    99

    Celsius-Fahreneit program

    I got the following code i written for an exercise of an a book
    Code:
    #include <stdio.h>
    
    main()
    {
    int ce;
    int fa;
    printf("Write fahreneit: ");
    scanf("%i", &fa);
    ce= (5\9)*(fa-32);
    printf("The celsius are %i\n", ce);
    }
    i don't get any error when compiling vut when running i get
    The celsius are 0
    i get this by writing differnet numbers, only 0, why does this happens?

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    - click here and here

    - remember the right division operator


    there you go , your program works now..


    two more things :

    - COMPILE the code before you put it here.

    - READ THE GOD DAMN ERRORS YOUR COMPILER SHOW !!!!!!! its like you're saying "there , YOU compile my code , YOU read the errors and YOU fix it"
    Last edited by Brain Cell; 12-09-2004 at 04:24 AM. Reason: typo
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    i don't get any error when compiling
    i don't get any erros

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    How would it compile with an error like that? :
    Code:
    ce= (5\9)*(fa-32);
    both VC++ and Dev C++ shows that error and wouldn't compile before you fix it

    whats your compiler anyway?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Both the formula in the OP for converting fahrenheit to celsius and the link provided by Brain Cell are correct. The OP uses C=(5/9)*(F-32). The link uses C=(F-32)/9*5. These are essentially the same thing (one form happens to be better than the other for purposes of programming however), so...

    Your problem comes from trying to do the integer division of 5 by 9 (5/9). This will always be 0 and your equation therefore becomes:

    Code:
    ce= 0*(fa-32);
    Which I hope you can see will also always be 0 no matter what fa happens to be. 0 times anything is still 0.

    Use floating point variables (float or double) to store your values and do your computations with, i.e.:

    Code:
    float ce;
    float fa;
    printf("Write fahrenheit: ");
    scanf("%f", &fa);
    ce= (5.0f/9.0f)*(fa-32);
    printf("The celsius is %f\n", ce);
    Last edited by hk_mp5kpdw; 12-09-2004 at 06:49 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    ok,
    i get it:
    whats your compiler anyway?
    my compiler is gcc!

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    turn the wanrings on gcc

  8. #8
    * S T U D E N T *
    Join Date
    Oct 2003
    Posts
    30

    Thumbs up

    Quote Originally Posted by cogeek
    I got the following code i written for an exercise of an a book
    Code:
    #include <stdio.h>
    
    main()
    {
    int ce;
    int fa;
    printf("Write fahreneit: ");
    scanf("%i", &fa);
    ce= (5\9)*(fa-32);
    printf("The celsius are %i\n", ce);
    }
    i don't get any error when compiling vut when running i get
    The celsius are 0
    i get this by writing differnet numbers, only 0, why does this happens?
    gcc -Wall -o file file.c (thats a good start (Wall means turn all warnings on) this is what i get:

    guyattc2-practical_c3-> gcc -Wall -o file file.c
    file.c:4: warning: return type defaults to `int'
    file.c: In function `main':
    file.c:9: error: stray '\' in program
    file.c:9: error: syntax error before numeric constant

    for a start helps to assign main() to an int as this is what this default function returns. To exit a program properly is exits by the statement:

    return(0);

    also errors already spotted: you need to use floats and also divide is '/' not '\'. Also, a 'nicer' way that ive been told to use with user input is to use fgets and sscanf as well. This is how my book explains it (im a newbie too ). scanf is described as 'dodgy'. So my friend, here is my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    
      float celsius;     /* celsius */
      float fahrenheit;  /* fahrenheit */
      char  line[50];    /* a line to take user input */
      
      printf("Write fahreneit: ");
      
      fgets(line, sizeof(line), stdin); /* take user input and store in 'line' */
      
      sscanf(line, "%f", &fahrenheit); /* take the input and find the float
                                           ("%f") and store this value in
    				       variable fahrenheit
    				    */
      
      celsius = (5.0f/9.0f) * (fahrenheit - 32);
      
      printf("Fahrenheit %f == Celsius %f\n", fahrenheit, celsius);
      
      return(0); /* return '0' to exit program normally */
    }
    With this code, i get this on the command:

    guyattc2-practical_c3-> gcc -Wall -o file file.c
    guyattc2-practical_c3-> ./file
    Write fahreneit: 32
    Fahrenheit 32.000000 == Celsius 0.000000

    hope this solves lots of problems!

    --CHriS
    'rm -fr /bin/laden'
    'kill all'

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    thanks,
    nice example

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. completed a program; desire input
    By spirited in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 08:50 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM