I'm having problems with the scanf();
function. When reading two or more values, subsequent values are not read.
I've already tried:
__fpurge(stdin);
After doing the readings, but in this case, I need to give an enter after each reading, which is a bit uncomfortable for me.
I've already tried it:
fflush(stdin);
But it does not solve (I'm using Debian 7.1, I think fflush(stdin);
only works on Windows)
What solved the problem was:
#include <stdio.h>
//Limpa o buffer do teclado
void flush_in(){
int ch;
while( (ch = fgetc(stdin)) != EOF && ch != '\n' ){}
}
int main(){
char c;
printf("\nEntre com um caractere: ");
scanf("%c",&c);
flush_in();
printf("\nO caractere: \"%c\" tem o valor ASCII %d", c ,(int)c);
printf("\nEntre com um caractere: ");
scanf("%c",&c);
flush_in();
//__fpurge ( stdin );
printf("\nO caractere: \"%c\" tem o valor ASCII %d", c ,(int)c);
printf("\nPress any key to exit...\n\n");
getchar();
return 0;
}
But the code for the function flush_in()
, I got here , it's obscure for me.
Then ...
What does the following code snippet do?
void flush_in(){
int ch;
while( (ch = fgetc(stdin)) != EOF && ch != '\n' ){}
}