How do i make a validating of only number and make characters invalid if entered. Using this method

    int main()
        int arr [100],el,number ;
    //  for(int i=0;i<100;++i)

        while( el == 0 ) 
                printf("Enter a number between 1 to 100");
                scanf("%d", &number );
                if( (number < 1) || (number > 100) )
                    printf("Number is not valid\n");
                    el = 1;
            printf("Number is %d\n", number );

        printf("Want to continue? (Y/N): ");
        if ((el=='Y') || (el == 'N') )
        printf("\007ERROR: Invalid choice");*/
        return 0;

Have a look at the functions isdigit, isalpha, isalnum etc.
Perhaps work out an IsNumber function with those.

Edited 2 Years Ago by ddanbe: addition

strtol is very flexible this way. You can give it a pointer to a string, a pointer to a string that will point to the last converted chartacter + 1, and a base to use in the conversion.

Using the second argument you can verify that some number of digits were converted and whether that conversion ended on a whitespace boundary or not (i.e. you can distinguish 1234RRTS and 1234 RRTS).

This article has been dead for over six months. Start a new discussion instead.