I read more threads about scanf and I found some answers bot none helped me:
while(!comanda){
int tmp;
if (scanf("%d", &tmp) == 0)
getchar();
else{
comanda = tmp;
fprintf(stdout,"%d",&comanda);
fflush(stdout);}
}
}
The problem is that after this lines of code get executed, nothing happens. After this I have a check on “comanda” which does not execute.
One of the problems with
scanfand all of the formatted input functions is that terminals tend to operate in line mode or cooked mode and the API is designed for raw mode. In other words,scanfimplementations generally will not return until a line feed is encountered. The input is buffered and future calls toscanfwill consume the buffered line. Consider the following simple program:You can enter multiple numbers before pressing return. Here is an example of running this program.
Each call to
scanfread a single number from the input stream but the first call did not return until after I pressed return. The remaining calls returned immediately without blocking for more input because the input stream was buffered and it could read another integer from the stream.The alternatives to this are to use
fgetsand processing entire lines of data at one time or using the terminal interface to disable “canonical input processing”. Most people usefgetssince the terminal interface section of POSIX is not implemented under Windows.