Hi,
first - need to initialize quit.
Then - put break when 'q' entered.
char quit=0;
while (quit != 'q'){
printf("Enter temperature in Fahrenheit (q to quit): ");
quit = getchar();
if(quit == 'q') break;
scanf("%lf", &f_temp);
Temperatures(f_temp);
}
"icarus" <rsarpi@[EMAIL PROTECTED]
> wrote in message
news:clcm-20080509-0009@[EMAIL PROTECTED]
> 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);
>
>
> }
> --
> comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
-- you
must
> have an appropriate newsgroups line in your header for your mail to be
> seen,
> or the newsgroup name in square brackets in the subject line. Sorry.
--
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
-- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.


|