RIDOUT@ddnvx1.afwl.af.mil (09/10/90)
The following is a little program I used while testing out DICE 2.05. I added the fflush(stdout) call to make this work the way I thought is should in the first place. Is I looked through the library source I found notes on how Matt uses a double buffer for output. My question is am I handleing this correctly or am I suposed to use a setvbuf call some how to make this work. Using setvbuf all I was able to accomplish is locking up my shell. This is DICE 2.05 unregistered if it makes a difference. The output without the fflush call lookes like the following: > test 12 2 Enter the speed in MPH: Enter the number of hours: Miles traveled = 24 > ----------------------------------------------------- #include <stdio.h> #include <stdlib.h> main() { int mph, time; printf("Enter the speed in MPH: "); fflush(stdout); scanf("%d",&mph); printf("Enter the number of hours: "); fflush(stdout); scanf("%d",&time); printf("Miles traveled = %d \n",mph * time); } ------------------------------------------------------ Thanks for any help. - **************************************************************************** * Brian Ridout Internet: ridout@ddnvx1.afwl.af.mil * * wl/scev * * Kirtland AFB NM 87117 My Apple is better than your Orange. * ****************************************************************************
dillon@overload.Berkeley.CA.US (Matthew Dillon) (09/11/90)
>In article <11479@ddnvx1.afwl.af.mil> RIDOUT@ddnvx1.afwl.af.mil writes: >The following is a little program I used while testing out DICE 2.05. >I added the fflush(stdout) call to make this work the way I thought is should >in the first place. Is I looked through the library source I found notes on >how Matt uses a double buffer for output. My question is am I handleing this >correctly or am I suposed to use a setvbuf call some how to make this work. >Using setvbuf all I was able to accomplish is locking up my shell. >This is DICE 2.05 unregistered if it makes a difference. Actually, it doesn't lock up your shell, but using setvbuf() to make the console unbuffered leaves it that way when the program exits unless you re-buffer it before exiting. control-J still works on the shell though returns are screwed up. >The output without the fflush call lookes like the following: > >> test >12 >2 >Enter the speed in MPH: Enter the number of hours: Miles traveled = 24 DICE does not automatically flush stdout when stdin is read. I am not sure how standard this is even though UNIX C does this and, apparently, Lattice and Manx do as well. I have come across several compilers that do not do the auto-flush and cannot find any ANSI reference so... what is right? The whole problem occurs when you do not write out a newline to stdout (which forces a flush)... in many cases, such as the program you outlined, you do not want to write out a newline but do want to flush the stdout buffer, and this is where the 'hack' comes into play (the auto-flushing when you read from stdin). >Thanks for any help. Basically, just keep the fflush()'s in. It is the most portable way of doing things. >- >**************************************************************************** >* Brian Ridout Internet: ridout@ddnvx1.afwl.af.mil * >* wl/scev * >* Kirtland AFB NM 87117 My Apple is better than your Orange. * >**************************************************************************** -- -Matt Matthew Dillon dillon@Overload.Berkeley.CA.US 891 Regal Rd. uunet.uu.net!overload!dillon Berkeley, Ca. 94708 USA