Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: pressing 'q...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 4 Topic 1098 of 1133
Post > Topic >>

Re: pressing 'q' or non-numeric char to exit loop/program

by gordonb.ig3f9@[EMAIL PROTECTED] (Gordon Burditt) May 13, 2008 at 01:05 AM

>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.
 




 4 Posts in Topic:
pressing 'q' or non-numeric char to exit loop/program
icarus <rsarpi@[EMAIL   2008-05-09 17:33:27 
Re: pressing 'q' or non-numeric char to exit loop/program
"micro" <mic  2008-05-13 01:05:18 
Re: pressing 'q' or non-numeric char to exit loop/program
gordonb.ig3f9@[EMAIL PROT  2008-05-13 01:05:23 
Re: pressing 'q' or non-numeric char to exit loop/program
David Given <dg@[EMAIL  2008-05-13 01:06:34 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 16:11:52 CDT 2008.