C Beginner - Fahrenheit and Celsius Converter

This is a discussion on C Beginner - Fahrenheit and Celsius Converter within the C Programming forums, part of the General Programming Boards category; I apologize, I am new to C programming. I know my current program will not compile. Can anyone give me ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    7

    C Beginner - Fahrenheit and Celsius Converter

    I apologize, I am new to C programming. I know my current program will not compile. Can anyone give me insight into what I can do to fix it? Specifically, how can I store the the start temperature so it can be used again in the final printf statement "start degrees Fahrenheit is converted Celsius."?

    Note - I want to use the float data type for precision.

    Code:
    //THIS PROGRAM WILL CONVERT TEMPERATURES BETWEEN DEGREES FAHRENHEIT AND DEGREES CELSIUS
    
    
    #include <stdio.h>
    
    
    int main(void)
    {
            //DECLARE VARIABLES
            char x;
            float start_temp, end_temp;
    
    
            //DISPLAY TO USER THE PURPOSE OF THE PROGRAM
            printf ("\n\n");
            printf ("This program will convert temperatures between degrees Fahrenheit and degrees Celsius.");
            printf ("\n\n");
    
    
            //ASK WHICH SYSTEM USER WOULD LIKE TO CONVERT
            printf ("Would you like to convert from Fahrenheit or Celsius (Enter C or F): ");
            scanf ("%c", &x);
            printf ("\n");
    
    
                     //IF FAHRENHEIT; ENTER TEMPERATURE                 
                    if (x == 'F' || x == 'f')
                    {
                    printf ("Enter the temperature in Fahrenheit (F): ");
                    scanf ("%f", &start_temp);
    
    
                    end_temp = (start_temp-32)/1.8;
    
    
                    //DISPLAY RESULT        
                    printf ("%f degrees Fahrenheit is %f Celsius.", start_temp, end_temp);
                    }
    
    
                    //IF CELSIUS; ENTER TEMPERATURE
                    else
                    {
                    printf ("Enter the temperature in Celsius (C): ");
                    scanf ("%f", &start_temp);
    
    
                    end_temp = start_temp * 1.8 + 32;
    
    
                    //DISPLAY RESULT
                    printf ("%f degrees Celsius is %f Fahrenheit.", start_temp, end_temp);
                    }
    
    
            printf ("\n\n");
    
    
            //END PROGRAM
    
    
            return 0;
    }
    Last edited by Spink85; 02-05-2013 at 05:36 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,690
    I didn't give the code a thorough look-over, but...

    I know my current program will not compile.
    It compiled fine for me, with no warnings or errors.

    Specifically, how can I store the the start temperature so it can be used again in the final printf statement "start degrees Fahrenheit is converted Celsius."?
    It already does this. Did you write this code yourself?

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    7
    I just tried to compile it again and it worked this time. Not sure what happened before. Sorry, I'm really new to this and I think I was thinking too hard about the final printf statement - I just needed the equation.

    I appreciate your help!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to our forum, Sprink85!

    Your use of code tags is good, and your indentation (which makes your code much easier to study), is good, until line 27, which should NOT be indented, and line 29, which SHOULD be indented. Just 3 to 5 spaces per indent level is great.

    You have the right logic, so I'm not clear what you're asking. Can you show output to show the problem? Post your errors.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    You might want to check to see if the input was ok - I noticed that you don't actually check if the input is c/C
    Code:
    //IF CELSIUS; ENTER TEMPERATURE
    else if (x == 'C' || x == 'c')
    {
      ...
    }
    //If user has entered something else
    else
    {
      puts("\nInvalid key");
    }
    Also with the scanf("%c"..), because this is your first input, it probably won't matter, but this will probably interest you - FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com - I would personally prefer the function getchar over scanf(" %c"...
    Last edited by Click_here; 02-05-2013 at 06:26 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fahrenheit to Celsius Converter
    By MariosX in forum C Programming
    Replies: 9
    Last Post: 09-02-2011, 05:36 PM
  2. Celsius in Fahrenheit converter - does not run
    By ApeWithGrape in forum C Programming
    Replies: 10
    Last Post: 03-06-2011, 11:01 PM
  3. Please help converting Fahrenheit to Celsius
    By victory1 in forum C Programming
    Replies: 16
    Last Post: 10-08-2009, 10:08 AM
  4. Conversion from Fahrenheit to Celsius problem
    By -Syntax in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2008, 08:56 PM
  5. celsius to fahrenheit conversion
    By chipster18 in forum C Programming
    Replies: 11
    Last Post: 03-26-2003, 07:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21