Your code has several issues:
1.
Your approach is incorrect.
You need to use if (scale == 'C')
instead of if (scale == C)
and if (scale == 'f')
instead of if (scale == F)
and omit the definition of C
and F
as char
variables.
'C'
is a character constant.
C
is, in your case, a uninitialized char
variable. It does not contain any specified value. Thus, the behavior of the program is undefined.
Same goes for F
.
2.
You should have an else
statement after if (scale == 'C')
and else if (scale == 'F')
which covers the case when the input provided is different than 'C'
or 'F'
(you will never know what the user may input).
3.
Furthermore, since scanf()
leaves a trailing newline character back in stdin
- Made by the press to Enter - you need to catch this newline character by f.e. getchar()
following each scanf()
statement to not skip the following input operation.
4.
main()
is not correct. Use at least int main()
, or even better int main (void)
to be standard-compliant.
5.
getch()
isn´t part of stdio.h
and even the C standard library in general. It is either a function from conio.h
(Windows/MS-DOS) or the ncurses-library (Linux). Thus, you need to include the specific header.
Probably it was a typo and you meant getchar()
instead, but worth to hint.
Side note:
You should always check the return value of scanf()
if the input was successfullly consumed.
Result:
#include <stdio.h>
int main (void)
{
float Fah, Cel, Temp;
char scale;
printf("Press F to convert to fahrenheit\n");
printf("Press C to convert in celcius\n");
scanf("%c", &scale);
getchar();
if (scale == 'C')
{
printf("Enter the temp\n");
if (scanf("%f", &Fah) != 1)
{
fputs("Wrong Input!", stderr);
return 1;
}
getchar(); // For catching newline character.
Temp = (Fah-32) / 1.8;
}
else if (scale == 'F')
{
printf("Enter the temp\n");
if (scanf("%f", &Cel) != 1)
{
fputs("Wrong Input!", stderr);
return 1;
}
getchar(); // For catching newline character.
Temp = (Cel * 1.8) + 32;
}
else
{
// Case for different input than 'F' and 'C'.
// Probably Error routine.
printf("Wrong input!\n");
}
printf("The temperature is %f\n", Temp);
getchar();
return 0;
}