Alright. I think I'm almost there. I'm just having a slight problem when I'm running the program.
This is how it should look like (for reference).
Enter a temperature: 32.4
Enter 'C' for Celsius or 'F' for Fahrenheit: C
Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): F
The temperature is: 90.3 F.
When I run it, after I enter 32.4, the next line automatically reads Enter 'C' for Celsius or 'F' for Fahrenheit: Unknown Syntax! without me having a chance to enter C or F in. And then it just takes me to the next line. I do something wrong in my switch statement?
Here's my code:
Code:
/* Directives */
#include <stdio.h>
int main(void)
{
float temp, CtoF, FtoC;
printf("Enter a temperature: ");
scanf("%f", &temp);
printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");
char choic;
choic=getchar();
switch(choic)
{
case 'F':
case 'f':
{break;
}
case 'C':
case 'c':
{break;
}
default:
{printf("Unknown Syntax!\n");
break;
}
}
printf("Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): ");
char choice;
choice=getchar();
switch(choice)
{
case 'F':
case 'f':
{CtoF = (temp - 32) * (5.0/9.0);
printf("The temperature is: %.1f F.\n", CtoF);
break;
}
case 'C':
case 'c':
{FtoC = temp * (5.0/9.0) + 32;
printf("The temperature is: %.1f C.\n", FtoC);
break;
}
default:
{printf("Uknown Syntax!\n");
break;
}
}
/* End Program */
return(0);
}