elder@wpafb-info2.arpa (ELDER.GREG) (12/31/87)
I'm fairly new to C and am trying to write a program using the Datalight Optimum-C compiler version 3.01 under MS-DOS Version 2.18. Here is the pixDeqce 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: " and "You entered <character>". Does anyone know why this happend? Why doesn't the prompt display and then wait for the user to press a key? Is there something unique about Datalight Optimum-C which causes this? Please reply directly to me as I am not on the INFO-C distribution list. Thanks in advance for any help. Greg Elder elder@wpafb-info2.arpa ------
ljz@fxgrp.UUCP (Lloyd Zusman, Master Byte Software) (01/01/88)
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 <stdio.h> 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