Thread: temperature

  1. #1
    Unregistered
    Guest

    temperature

    I have a problem

    I have to convert temperature between farenhiet and celsius.

    here is my code

    Code:
    #include <stdio.h>
    
    int main ()
    {
       double temp;
       int check;
    
       printf("Please enter a fahrenheit temperature ");
       check = scanf("%f", &temp);
       if (check != 1)
       {
          printf("Invalid input\n");
          return 0;
       }
    
       temp = (temp - 32.0) * 5.0 / 9.0;
       printf("\nTemperature converted to celsius = %.1f\n", temp);
    
       return 0;
    }
    my problem is that i get the wrong results. When i enter 5 i should be getting -15.0 but instead i get 1120.0. Can someone help me.

  2. #2
    Unregistered
    Guest
    i found why the result stuffed up, i had to change the double to a float. Can any one tell me the difference between double and float? I always thought they were the same thing

  3. #3
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    here..

    Code:
    #include <stdio.h>
    
    main()
    {
    	float celcius, farenheit;
    
    	printf("Enter temp in Farenheit: ");
    	scanf("%f", &farenheit);
    
    	celcius = (farenheit - 32) * 5/9;
    	
    	printf("Celcius: %f\n", celcius);
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Can any one tell me the difference between double and float?
    Well it's important in the scanf

    scanf( "%f", &float_var );
    scanf( "%lf", &double_var );

    doubles typically take up twice as much memory as floats, but they are a lot more precise, and have a greater range than floats.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Temperature Conversion code problems
    By eroth88 in forum C Programming
    Replies: 6
    Last Post: 10-22-2006, 01:24 AM
  2. Motherboard Temperature
    By MK4554 in forum Windows Programming
    Replies: 1
    Last Post: 07-18-2006, 10:51 AM
  3. Temperature conversion...
    By Onslaught in forum C Programming
    Replies: 3
    Last Post: 10-21-2005, 01:15 PM
  4. read the CPU temperature
    By BianConiglio in forum Windows Programming
    Replies: 2
    Last Post: 05-19-2004, 11:41 AM
  5. functions ??? help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2001, 02:33 AM