# Thread: C Beginner - Fahrenheit and Celsius Converter

1. ## 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;
}```

2. 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. 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.

4. 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. 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"...