single character input

Kral braun at drivax.UUCP
Thu May 26 06:33:20 AEST 1988


Allow me to reply to several articles at once.

Several people started out their postings with "Perhaps I missed something...".
I think what you missed was that I had started out with scanf() to get the
single character input.  I got the type of interaction described by
peter at athena.mit.edu (Peter J Desnoyers) in <5455 at bloom-beacon.MIT.EDU>:

:...and at least one person I know
:got caught by this same bug. They wrote:
:
:  for (...) { printf( "y/n:"); scanf( "%c", &answer); ...}
:
:Unfortunately, what they got was:
:    value read:		input typed:
:       'y'		    'y\n'	(note - scanf will not return until
:       '\n'		    nothing      you hit return)
:
:and so forth. Since they were testing only against 'y', they never noticed
:that the non-y value wasn't a 'n', and got seriously confused.
:

Also note: the whole idea of using scanf() in the first place was so that I
could use a previously written library routine to read the line (the system
handles the line editing (backspace, etc)) scan it for the correct pattern
(in this case, a single character input) and return an error if anything (and
I mean *anything*, like multi-character input, etc) else was entered.

Also, davidsen at steinmetz.ge.com (William E. Davidsen Jr), in Article
<10900 at steinmetz.ge.com>, says:
:
:Perhaps I misunderstand your problem. It sounds as if you want to read
:the first character on a line and discard the rest.
:
:	int read1() {
:	  register int ch1, ch2;
:	  
:	  ch1 = ch2 = getchar();
:	  while (ch2 != '\n') ch2 = getchar();
:	  return(ch1);
:	}

Correct.  And, in fact, I did this for a while, before discarding it in favor
of using gets() instead.  It does the same thing.  In either case, you don't
get a return from the function until after the newline has been read by the
system.  This eliminates the need for me to have to write code to handle
backspaces, and suggested by dheeraj at sdag.cs.umd.edu (Dheeraj Sanghi) in
Article <11586 at mimsy.UUCP>.

In Article <6151 at sigi.Colorado.EDU>, swarbric at tramp.Colorado.EDU (Frank 
Swarbrick), says:
:
:I must have missed something along the line somewhere.  ch = getchar() works
:fine for me.  I can use backspace to edit it.  I can type as many characters as
:I want.  When I finally press return the first character that is left is
:assigned to ch.
:
:Is this not the way getchar() works on some machines/compilers?
:

The problem is this: if the first character input is not a valid input, what do
you do?  You loop back, redisplay the prompt (probably with some sort of error
message along the way) and read again.  What you will get is the next character
input on the old line, which will probably also be wrong, and the process will
continue until all of the character have been read.  The result is a bunch of
error messages displayed because the user entered one bad input line.

I guess the proper thing to do, then, is either write my own function (which I
probably will do), or use gets and sscanf().

Thanx for all of your comments, both posted and emailed.


-- 
kral 	408/647-6112			...{ism780|amdahl}!drivax!braun
"I'll let you be in my dream                      If I can be in yours"
DISCLAIMER: If DRI knew I was saying this stuff, they would shut me d~-~oxx



More information about the Comp.lang.c mailing list