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 4 of 4 Topic 1098 of 1134
Post > Topic >>

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

by David Given <dg@[EMAIL PROTECTED] > May 13, 2008 at 01:06 AM

icarus wrote:
[...]
> 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.

Your main problem is here:

[...]
> 		quit = getchar();
> 		scanf("%lf", &f_temp);

When you call any stdio input function, the system will stop and wait
for the user to enter a line of text and press RETURN. They will then
work on this line of text, each function consuming characters from the
buffer, until the buffer is empty, at which point the system will stop
and wait for another line of text.

So when getchar() is called, the system stops and waits for the user to
type something in. Say, "123.456\n".

getchar() then consumes one character, returns '1', and the buffer is
"23.456\n". scanf() then assigns that to f_temp, leaving the buffer
containing "\n".

But if the user typed "q\n", then getchar() returns 'q', leaving the
buffer containing "\n"; scanf will look at this buffer, see that there
is nothing that looks like a number in it, and return an error code
leaving f_temp unchanged.

If this happened the first time through, then f_temp is undefined, and
Temperatures() will produce garbage. (It shouldn't hang, though.) If it
happens on a subsequent iteration, then f_temp will retain its old
value, which is why you're seeing the same value repeated.

Try, instead, using fgets() to read a complete line from the keyboard
into a buffer, then using strcmp() and sscanf() to parse that buffer,
rather than trying to parse stuff on-the-fly from the keyboard.

(Further problems involve not initialising your variables --- remember
that an uninitialised variable can contain *any* value, so if quit has
'q' on entry to your function, you're out of luck; you should also check
the result of scanf() to ensure that it found valid data.)

-- 
┌─── dg@cowlark.com ─────
http://www.cowlark.com
─────
│ "I have always wished for my computer to be as easy to use as my
│ telephone; my wish has come true because I can no longer figure out
│ how to use my telephone." --- Bjarne Stroustrup
-- 
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 Fri Jul 25 15:59:35 CDT 2008.