how to check if the input is a number or not in C?

前端 未结 8 2003
暖寄归人
暖寄归人 2020-11-27 05:38

In the main function of C:

void main(int argc, char **argv)
{
   // do something here
}

In the command line, we will type any number for ex

相关标签:
8条回答
  • 2020-11-27 05:52

    Using scanf is very easy, this is an example :

    if (scanf("%d", &val_a_tester) == 1) {
        ... // it's an integer
    }
    
    0 讨论(0)
  • 2020-11-27 05:55

    I was struggling with this for awhile, so I thought I'd just add my two cents:

    1) Create a separate function to check if an fgets input consists entirely of numbers:

    int integerCheck(){
    char myInput[4];
    fgets(myInput, sizeof(myInput), stdin);
        int counter = 0;
        int i;
        for (i=0; myInput[i]!= '\0'; i++){
            if (isalpha(myInput[i]) != 0){
                counter++;
                if(counter > 0){
                    printf("Input error: Please try again. \n ");
                    return main();
                }
            }
    
        }
        return atoi(myInput); 
    }
    

    The above starts a loop through every unit of an fgets input until the ending NULL value. If it comes across a letter or an operator, it adds "1" to the int "counter" which is initially set to 0. Once the counter becomes greater than 0, the nested if statement instructs the loop to print an error message & then restart the program. When the loops completes, if int 'counter' is still the value of 0, it returns the initially inputted integer to be used in the main function ...

    2) the main function would be:

    int main(void){
    unsigned int numberOne;
    unsigned int numberTwo;
    numberOne = integerCheck();
    numberTwo = integerCheck();
    return numberOne*numberTwo;
    
    }
    

    Assuming both integers are inputted correctly, the example provided will yield the result of int "numberOne" multiplied by int "numberTwo". The program will repeat for however long it takes to get two properly inputted integers.

    0 讨论(0)
  • 2020-11-27 05:55
    if (sscanf(command_level[2], "%f%c", &check_f, &check_c)!=1)
    {
            is_num=false;
    }
    else
    {
            is_num=true;
    }   
    
    if(sscanf(command_level[2],"%f",&check_f) != 1) 
    {
        is_num=false;
    }
    

    how about this?

    0 讨论(0)
  • 2020-11-27 06:03

    Using fairly simple code:

    int i;
    int value;
    int n;
    char ch;
    
    /* Skip i==0 because that will be the program name */
    for (i=1; i<argc; i++) {
        n = sscanf(argv[i], "%d%c", &value, &ch);
    
        if (n != 1) {
            /* sscanf didn't find a number to convert, so it wasn't a number */
        }
        else {
            /* It was */
        }
    }
    
    0 讨论(0)
  • 2020-11-27 06:06

    You can use a function like strtol() which will convert a character array to a long.

    It has a parameter which is a way to detect the first character that didn't convert properly. If this is anything other than the end of the string, then you have a problem.

    See the following program for an example:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main( int argc, char *argv[]) {
        int i;
        long val;
        char *next;
    
        // Process each argument given.
    
        for (i = 1; i < argc; i++) {
            // Get value with failure detection.
    
            val = strtol (argv[i], &next, 10);
    
            // Check for empty string and characters left after conversion.
    
            if ((next == argv[i]) || (*next != '\0')) {
                printf ("'%s' is not valid\n", argv[i]);
            } else {
                printf ("'%s' gives %ld\n", argv[i], val);
            }
        }
    
        return 0;
    }
    

    Running this, you can see it in operation:

    pax> testprog hello "" 42 12.2 77x
    
    'hello' is not valid
    '' is not valid
    '42' gives 42
    '12.2' is not valid
    '77x' is not valid
    
    0 讨论(0)
  • 2020-11-27 06:07

    Another way of doing it is by using isdigit function. Below is the code for it:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define MAXINPUT 100
    int main()
    {
        char input[MAXINPUT] = "";
        int length,i; 
    
        scanf ("%s", input);
        length = strlen (input);
        for (i=0;i<length; i++)
            if (!isdigit(input[i]))
            {
                printf ("Entered input is not a number\n");
                exit(1);
            }
        printf ("Given input is a number\n");
    }
    
    0 讨论(0)
提交回复
热议问题