I've got in one program filtered input via scanf. I want read only numbers and big letters + spaces.
Recently I was trying to do something like that, and it stuck in an infinite loop, still printing only first input.
I know about fgets()
, but I know this worked.
I can't find out where the problem is.
Here's simplified situation; what is happening to me?
#include <stdio.h>
#include <string.h>
int main()
{
char str[21];
do
{
scanf("%20[0-9A-Z ]", str);
printf("%s\n", str);
} while(strcmp("END", str) != 0);
return 0;
}
EDIT: I forgot to mention, input is valid, for input: "HELLO" program stuck...and scanf
return 1 only for first input, for others returning 0.
And I also tried fflush(stdin)
, after reading...
getc(stdin)
and check the character till it gets 20 valid characters. – Shiplu Mokaddimsscanf()
– Wolf