Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!lll-tis!ames!fxgrp!ljz From: ljz@fxgrp.UUCP (Lloyd Zusman, Master Byte Software) Newsgroups: comp.lang.c Subject: Re: No Echo Input Under MS-DOS Message-ID: <192@fxgrp.UUCP> Date: 31 Dec 87 18:48:31 GMT References: <11055@brl-adm.ARPA> Reply-To: fxgrp!ljz@ames.arpa (Lloyd Zusman, Master Byte Software) Followup-To: <11055@brl-adm.ARPA> elder@wpafb-info2.arpa (ELDER.GREG) Organization: FX Development Group, Inc., Mountain View, CA Lines: 58 In article <11055@brl-adm.ARPA> elder@wpafb-info2.arpa (ELDER.GREG) writes: > ... >I'm fairly new to C and am trying to write a program using the >Here is the pixDeqce [sic] of code I am having trouble with: > > printf("Enter a key: "); > key = getch(); /* Get keyboard character without echoing */ > printf("\nYou entered %c\n", key); > >When I run the program the screen remains blank until I hit a key. Only >after pressing a key does the program then respond with "Enter a key: " > ... Try putting the following statement after the first printf() call: fflush(stdout); Many implementations do not flush the output buffer until a specified number of characters (often 512) or a newline is sent, so you would have to do an explicit flush to get the string out to the terminal. It also should flush when any input is done via getchar(), gets(), fgets(), fgets(), etc., but you aren't using any of these here. The second printf() call in your example contains newlines and this causes the first stuff to get flushed. The getch() call under MSDOS references a low-level operating system function and bypasses the standard C I/O processing you would get via getchar(), gets(), etc. You need this function so that the user doesn't have to type a carriage return after entering the character, but the side-effect is that it isn't part of the C I/O processing and hence doesn't do the flushing you would like it to do. The fflush() solves this problem. Before any of you unix people complain, I might point out that the Datalight C compiler under MSDOS has no analog to the ioctl() unix calls for setting the tty mode. Special MSDOS-specific routines, one of which being getch(), are needed to do "raw" terminal I/O. In case you don't know this, 'stdout' is the place that printf() writes to, which is why it is used as the argument to fflush(). I'm pretty sure that this would solve your problem. Also, you would need #include before this code in order that the compiler knows what is meant by 'stdout'. I suggest this because the code fragment you set us here would work just fine without the inclusion of stdio.h, and hence you might not have included it. Good luck. ------------------------------------------------------------------------- Lloyd Zusman Master Byte Software Los Gatos, California Internet: fxgrp!ljz@ames.arpa "We take things well in hand." UUCP: ...!ames!fxgrp!ljz