Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!ames!ll-xn!mit-eddie!uw-beaver!fluke!ssc-vax!dmg From: dmg@ssc-vax.UUCP (David Geary) Newsgroups: comp.lang.c Subject: C Quirk?? Message-ID: <1653@ssc-vax.UUCP> Date: 3 Feb 88 20:40:41 GMT Organization: Boeing Aerospace Corp., Seattle WA Lines: 64 Keywords: Weird_Behavior() I've been wondering about this for a long time. Maybe someone out there can shed some light on the subject... #include #include main() { char ch; puts("Enter a Character: "); while( (ch = getchar()) ) { if( isupper(ch) ) tolower(ch); printf("%c\n", ch); puts("Enter a Character: "); } } Here's the output that I get when running the above program: Enter a Character: a A Enter a Character: Enter a Character: The first time, everything is ok. The next time, however, it seems to just blow right by the getchar(). This is what I think: 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()? I love to program in C, but this p*sses me off. Thanks in advance for the replies. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ "...You can't always get what you want..." ~ ~ Rolling Stones ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- *********************************************************** * David Geary, Boeing Aerospace Co., Seattle, WA * * (206)773-5249. --I disclaim everything I've ever said--* ***********************************************************