I
icarus
Hi, this is a simple temperature converter (Fahrenheit to Celsius to
Kelvin). Using gcc 4.0.
The user is supposed to enter q or any other non-character to exit the
program.
Problem with this code:
I enter 'q' as soon as the program starts. It hangs.
Then run it again. Enter a number, it executes fine. But when I enter
q, it repeats last number and it doesn't 'pay attention' to the letter
entered.
Any ideas on how can I fix it? thanks in advance.
#include <stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;
while (quit != "q"){
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();
scanf("%lf", &f_temp);
Temperatures(f_temp);
}
printf("\nbye!");
return 0;
}
void Temperatures(double f_temp){
double celsius, kelvin;
celsius = (F_TO_CELS_ONE_EIGHT * f_temp) + F_TO_CELS_THIRTYTWO;
kelvin = celsius + C_TO_KELVIN;
printf("Fahrenheit degrees: %.2f\n", f_temp);
printf("Celsius degrees: %.2f\n", celsius);
printf("Kelvin degrees: %.2f\n", kelvin);
}
Kelvin). Using gcc 4.0.
The user is supposed to enter q or any other non-character to exit the
program.
Problem with this code:
I enter 'q' as soon as the program starts. It hangs.
Then run it again. Enter a number, it executes fine. But when I enter
q, it repeats last number and it doesn't 'pay attention' to the letter
entered.
Any ideas on how can I fix it? thanks in advance.
#include <stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;
while (quit != "q"){
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();
scanf("%lf", &f_temp);
Temperatures(f_temp);
}
printf("\nbye!");
return 0;
}
void Temperatures(double f_temp){
double celsius, kelvin;
celsius = (F_TO_CELS_ONE_EIGHT * f_temp) + F_TO_CELS_THIRTYTWO;
kelvin = celsius + C_TO_KELVIN;
printf("Fahrenheit degrees: %.2f\n", f_temp);
printf("Celsius degrees: %.2f\n", celsius);
printf("Kelvin degrees: %.2f\n", kelvin);
}