Validating input with isdigit for a factorial program

You are using isdigit wrong. Read its documentation to find out what it actually does.

You probably meant:

 if ( number >= 0 && number <= 9 )

However you also need to check whether scanf succeeded or not. If they type in some words, then scanf("%d" fails and does not update number, so trying to access number in that case accesses an uninitialized variable. To deal with that you could either check the return value of scanf, or do:

int number = -1;

because the value will be left unchanged if the input failed.

NB. Don’t use fflush(stdin)

Browse More Popular Posts

Leave a Comment