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 scanf
and 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, scanf
implementations generally will not return until a line feed is encountered. The input is buffered and future calls to scanf
will consume the buffered line. Consider the following simple program:
#include <stdio.h>
int main() {
int a_number;
printf("Enter a number: ");
fflush(stdout);
while (scanf("%d", &a_number) != EOF) {
printf("you entered %d\n", a_number);
printf("Enter another number: ");
fflush(stdout);
}
return 0;
}
You can enter multiple numbers before pressing return. Here is an example of running this program.
bash$ gcc foo.c
bash$ ./a.out
Enter a number: 1 2 3 4 5 6 7 8 9 10<Return>
you entered 1
Enter another number: you entered 2
Enter another number: you entered 3
Enter another number: you entered 4
Enter another number: you entered 5
Enter another number: you entered 6
Enter another number: you entered 7
Enter another number: you entered 8
Enter another number: you entered 9
Enter another number: you entered 10
Enter another number: <Ctrl+D>bash$
bash$
Each call to scanf
read 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 fgets
and processing entire lines of data at one time or using the terminal interface to disable "canonical input processing". Most people use fgets
since the terminal interface section of POSIX is not implemented under Windows.
Your scanf("%d", &tmp)
can return one of 3 values
- if it returns
1
it means a value was read and placed in tmp
- if it returns
0
it means there was a bad character in buffer (which you detect and get rid of with the next getchar()
)
- if it returns
EOF
it means stdin
is at end-of-file condition. No matter how many getchar()
s you do, the 'end-of-file' condition is not going away and you're stuck in an infinite loop.
Also test the return value from scanf for EOF.
while(!comanda){
int tmp;
int ret;
ret = scanf("%d", &tmp);
if (ret == 0)
getchar();
else if (ret == EOF){
perror("stdin end-of-file");
break; /* get out of the loop */
}
else {
comanda = tmp;
fprintf(stdout,"%d",comanda); /* & is an error */
fflush(stdout);}
}
}
Or, even better, redo your program to read a full line with fgets()
and parse it with sscanf()
.