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 > pressing 'q' or...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 1098 of 1145
Post > Topic >>

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

by icarus <rsarpi@[EMAIL PROTECTED] > May 9, 2008 at 05:33 PM

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.
 




 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 Sep 4 23:58:25 CDT 2008.