Hi all,
I am pretty new to C programming, I used Pascal before.
I've tried to write a program which converts between Fahrenheit and Celsius.
But when I ask the user to write a char, the program doesnt recognize it. I did a simple test, and I found, that my program saves the enter (\n) as well as the char. But that's not supposed to be. So how to solve this?
I used Borland C++ Builder.
Thanks for any help!
Code:
#include <stdio.h>
int main (void)
{
char CorFahr;
float convertable;
printf("Convert celsius or fahrenheit? c-f");
do
{
CorFahr=getchar();
printf("%c",CorFahr);
if (CorFahr == 'c' || CorFahr == 'f')
{
printf("write c or f");
}
}
while (CorFahr != 'c' || CorFahr != 'f');
scanf("%f",&convertable);
if (CorFahr == 'c' )
{
printf("%f in celsius: ",convertable);
convertable-=32;
convertable*=5/9;
printf("%f\n", convertable);
}
if (CorFahr == 'f')
{
printf("%f in fahrenheit: ",convertable);
convertable*=9/5;
convertable+=32;
printf("%f\n",convertable);
}
return 0;
}