Path: utzoo!utgpu!water!watmath!clyde!rutgers!rochester!cornell!batcomputer!sun!mrd From: mrd@sun.soe.clarkson.edu (Mike DeCorte) Newsgroups: comp.lang.c Subject: Re: C Quirk?? Message-ID: <373@sun.soe.clarkson.edu> Date: 5 Feb 88 05:52:04 GMT References: <1653@ssc-vax.UUCP> Organization: Clarkson University, Potsdam, NY Lines: 34 In-reply-to: dmg@ssc-vax.UUCP's message of 3 Feb 88 20:40:41 GMT Posting-Front-End: GNU Emacs 18.47.5 of Mon Jan 25 1988 on sun.soe.clarkson.edu (berkeley-unix) In article <1653@ssc-vax.UUCP> dmg@ssc-vax.UUCP (David Geary) writes: It asks me to "Enter a Character: ". I type 'a', and then a newline. getchar() grabs the 'a' from stdin, but leaves the newline hanging. The next time getchar() comes around, it grabs the newline that was left hanging around last time. Note that if I insert fflush(stdin) before I do if( isupper(ch) ), everything works correctly. Is this a *bug* in C, or is it a *feature*. Am I interpreting events correctly? Is there a fix besides fflush()? First off. You have the line tolower(ch); in your text as a statement. This does nothing. tolower() (as well as all of the things in ctype.h) return values. In this case it returns the lower case of ch but ch itself is not modified. second: You are 100% correct on what C is doing with your input. You see getchar gets ONE character, not one character and a '\n'. The first call grabs 'C' the second call grabs '\n'. The logical response to this is "well how do I tell this thing that I don't want to type return?" What the tty driver is doing is buffering your I/O. To turn off buffering is well system dependent. On unix one uses ioctl(2) on anything else - I don't know. -- Michael DeCorte // mrd@clutx.clarkson.edu // mrd@clutx.bitnet (315)268-4704 // P.O. Box 652, Potsdam, NY 13676