Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!auspex!guy From: guy@auspex.auspex.com (Guy Harris) Newsgroups: comp.lang.c Subject: Re: learning c Keywords: getchar() Message-ID: <1384@auspex.auspex.com> Date: 4 Apr 89 09:24:31 GMT References: <1091@bnlux0.bnl.gov> <584@greens.UUCP> Reply-To: guy@auspex.auspex.com (Guy Harris) Distribution: usa Organization: Auspex Systems, Santa Clara Lines: 30 > I know your problem well. What you are looking for is a 'getch()' or >'getche()' function in place of the 'getchar()' function. Huh? What would those functions do? They're not in the ANSI C standad nor are they in any UNIX C library. If they're a PCism, note that plenty of C programs have never programmed on a PC and, as such, may not be familiar with them. >Barring that try > >c = getchar(); >fflush(stdin); > > The variable 'c' will get the first character of input, and the remainder >of the line (plus the newline) will get flushed away. Bad idea, as Rahul Dhesi has already pointed out: 1) "fflush(stdin)" is not guaranteed to flush input on all implementations; 2) even if it does do so on your implementation, it may or may not stop at the end of the line in all cases. Much better to *read* the rest of the line, as in Rahul's posting. Not only is it portable, it's the right thing to do - flushing input is generally an action taken on an error, not something you do as a matter of course unless you want to make *sure* somebody hasn't typed something ahead that they'll regret (cf. "rn", which does flush input for precisely that reason).