absolute beginner here. This is actually my first little coding project I've done after a small amount of reading on c. here's my code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float temperature;
float cels, fahr, kel;
printf("Please enter your temperature.\n\n");
scanf("%f", &temperature);
fflush(stdin);
printf("\n\nIs this in fahrenheit, celsius, or kelvin? (type f,c, or k)\n");
char c;
c = getchar();
if (c == 'f')
fahr = temperature;
else if(c == 'c')
cels = temperature;
else if(c == 'k')
kel = temperature;
if (fahr = temperature){
cels = (5*(fahr-32))/9;
kel = cels +273;
}
else if (cels = temperature){
kel = cels + 273;
fahr = ((9 * cels)/5)+32;
}
else if (kel = temperature){
cels = kel - 273;
fahr = ((9 * cels)/5)+32;
}
printf("\nOkay, so the temperatures are:\n");
printf("fahr: %f\tcels: %f\tkal: %f\n" , fahr, cels, kel);
return 0;
}
The code is supposed to ask for a temperature value, check based on a user input if that temperature is in fahrenheit, celsius or kelvin, then it finds what the input temperature equals in the other terms, and outputs those values.
I'm pretty sure the problem I'm running into is that the buffer still has an value when I use c = getchar to see if the temperature is in fahr, cels, or kel, therefore the program doesn't wait for a user input at that line of code. I know now that fflush(stdin); is not ideal at all in clearing the buffer and I'm not sure why (like I said, complete beginner here). Anyways any enlightenment into clearing the buffer or even more on how the buffer input works would be greatly appreciated. Thanks.