i need to calculate the age then in c programming how can i do “user input filtering” at scanf.
main()
{
scanf("%d %d %d ,month,date, year);
--------;
printf("your age is"...);
}
here the user can enter alphabets or some symbols how to filter them in c?
If by filtering, you mean remove the offending characters, you can either read lines in buffers and inspect them, or use a quick hack like this:
where the
*after%means to read and throw away and the^0-9in brackets means any character that is not 0 through 9. Possible pitfall: if the first character is unwanted,scanffails. If you put a%*[^0-9]in the beginning, then input beginning with a number fails.Alternatively, ask the user for proper input in case of failure (in previous case also you should check for the return value of
scanf):