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

 #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<ctype.h>
    int main()
    {
        int arr [100],el,number ;
    //  for(int i=0;i<100;++i)
        //scanf("%d",arr[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");
                else
                    el = 1;
            }
            printf("Number is %d\n", number );
            fflush(stdin);


            /*el=0;
        while(el=0)
        printf("Want to continue? (Y/N): ");
        scanf("%c",&el);
        el=toupper(el);
        if ((el=='Y') || (el == 'N') )
        el=1;
        else 
        printf("\007ERROR: Invalid choice");*/
        return 0;
}

Recommended Answers

All 2 Replies

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

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.