Thread: Temperature conversion...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Temperature conversion...

    Hi, I'm new at C programming, and I'm following the HowStuffWorks.com C programming tutorial/guide until I get some books- it has nice examples and lets you experiment. Anyways, I'm working on a temperature conversion program- a basic loop project. I'm trying to convert Fahrenheit to Celcius with user-inputted starting temp, ending temp, and increment value. Problem is, IT KEEPS REPEATING. I've spent an hour trying to figure out why, but i can't find the cause of it all. So here's the code. Thanks

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    float a, b, c, d;
     printf("Enter a value for the starting temperature in Fahrenheit:");
       scanf("%f", b);
       printf("Enter a value for the ending temperature in Fahrenheit:");
       scanf("%f", c);
       printf("Enter a value for the temperature increment value in Fahrenheit:");
       scanf("%f", d);
       a = b;
       printf("This is a Fahrenheit to Celcius Conversion Table!");
        while (a <= c)
        {
           if ((a > 98.6) && (a < (a + d)))
            {
    
               printf("%f degrees F = %f degrees C\n",
                   98.6, (98.6 - 32.0) * 5.0 / 9.0);
    
            }
    
            printf("%f degrees F = %f degrees C\n",
                a, (a - 32.0) * 5.0 / 9.0);
            a = (a + d);
    
        }
    
        return 0;
    
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You had a few minor errors that I fixed, this code works fine for me. You'll just want to format the output on your floats to whatever way you feel nessassary. I set the precision to two places.

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    float a, b, c, d;
     printf("Enter a value for the starting temperature in Fahrenheit:");
       scanf("%f", &b);     // You had b, should be &b
       printf("Enter a value for the ending temperature in Fahrenheit:");
       scanf("%f", &c);     // You had c, should be &c
       printf("Enter a value for the temperature increment value in Fahrenheit:");
       scanf("%f", &d);     // You had d, should be &d
       a = b;
       printf("This is a Fahrenheit to Celcius Conversion Table! \n"); // Don't forget your \n here
        while (a <= c)
        {
           if ((a > 98.6) && (a < (a + d)))
            {
    
               printf("%.2f degrees F = %.2f degrees C \n",
                   98.6, (98.6 - 32.0) * 5.0 / 9.0);
    
            }
    
            printf("%.2f degrees F = %.2f degrees C \n",
                a, (a - 32.0) * 5.0 / 9.0);
            a = (a + d);
            }
        
        return 0;
    }
    Here is the output:
    Code:
    Enter a value for the starting temperature in Fahrenheit:52
    Enter a value for the ending temperature in Fahrenheit:88
    Enter a value for the temperature increment value in Fahrenheit:1
    This is a Fahrenheit to Celcius Conversion Table!
    52.00 degrees F = 11.11 degrees C
    53.00 degrees F = 11.67 degrees C
    54.00 degrees F = 12.22 degrees C
    55.00 degrees F = 12.78 degrees C
    56.00 degrees F = 13.33 degrees C
    57.00 degrees F = 13.89 degrees C
    58.00 degrees F = 14.44 degrees C
    59.00 degrees F = 15.00 degrees C
    60.00 degrees F = 15.56 degrees C
    61.00 degrees F = 16.11 degrees C
    62.00 degrees F = 16.67 degrees C
    63.00 degrees F = 17.22 degrees C
    64.00 degrees F = 17.78 degrees C
    65.00 degrees F = 18.33 degrees C
    66.00 degrees F = 18.89 degrees C
    67.00 degrees F = 19.44 degrees C
    68.00 degrees F = 20.00 degrees C
    69.00 degrees F = 20.56 degrees C
    70.00 degrees F = 21.11 degrees C
    71.00 degrees F = 21.67 degrees C
    72.00 degrees F = 22.22 degrees C
    73.00 degrees F = 22.78 degrees C
    74.00 degrees F = 23.33 degrees C
    75.00 degrees F = 23.89 degrees C
    76.00 degrees F = 24.44 degrees C
    77.00 degrees F = 25.00 degrees C
    78.00 degrees F = 25.56 degrees C
    79.00 degrees F = 26.11 degrees C
    80.00 degrees F = 26.67 degrees C
    81.00 degrees F = 27.22 degrees C
    82.00 degrees F = 27.78 degrees C
    83.00 degrees F = 28.33 degrees C
    84.00 degrees F = 28.89 degrees C
    85.00 degrees F = 29.44 degrees C
    86.00 degrees F = 30.00 degrees C
    87.00 degrees F = 30.56 degrees C
    88.00 degrees F = 31.11 degrees C
    Last edited by SlyMaelstrom; 10-20-2005 at 11:16 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    Thanks a lot. Seemed to overlook the aspersand (sp?)... Works fine now!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    ampersand. Sort of like asterisk.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Temperature Conversion code problems
    By eroth88 in forum C Programming
    Replies: 6
    Last Post: 10-22-2006, 01:24 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Help with temperature conversion Chart
    By sanmaximo in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2003, 03:29 PM