>Problem with this code:
>I enter 'q' as soon as the program starts. It hangs.
You have a getchar() which reads the 'q'. What does the program
do next? scanf(), right? What is the user supposed to enter here?
>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.
I note that your program does not pay attention to the return
value of scanf().
>Any ideas on how can I fix it? thanks in advance.
Do you really know how the interaction between the user and the
program is supposed to work?
>#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"){
quit is a character. "q" is a character string. A comparison like this
isn't going to work. You will likely never get the two to compare equal,
although it could happen.
> printf("Enter temperature in Fahrenheit (q to quit): ");
> quit = getchar();
Perhaps you want to check *HERE* whether quit == 'q', and do something
sensible if it is? Also, decide what the user should enter if they
do NOT want to quit, and fix the prompt to accurately state that.
> scanf("%lf", &f_temp);
If I enter 32<ENTER> here, you end up with quit = '3' and f_temp
equal to 2.0 . Is this what you want? I doubt it.
> 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.


|