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.


|