Path: utzoo!attcan!uunet!husc6!mailrus!ames!amdahl!drivax!braun From: braun@drivax.UUCP (Kral) Newsgroups: comp.lang.c Subject: Re: single character input Message-ID: <3443@drivax.UUCP> Date: 25 May 88 20:33:20 GMT References: <3423@drivax.UUCP> <5455@bloom-beacon.MIT.EDU> Reply-To: braun@drivax.UUCP (Kral) Distribution: na Organization: Digital Research, Inc. Lines: 76 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@athena.mit.edu (Peter J Desnoyers) in <5455@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@steinmetz.ge.com (William E. Davidsen Jr), in Article <10900@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@sdag.cs.umd.edu (Dheeraj Sanghi) in Article <11586@mimsy.UUCP>. In Article <6151@sigi.Colorado.EDU>, swarbric@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