-3

I need to determine whether or not a user input is numeric. I need to loop over each characters and check if all are digits, if not printf() Error message, or else Successful. I used a for loop to loop over each characters and an if statement with isdigit() function to check the digit. But my problem is I am printing for each characters. For example, if I enter 22A, I get 2 Successful messages for 2 and 2, and then I get Error message for A. But I need just print the error message since 22A is not entirely numeric.

    int n = strlen(argv[1]);
    for (int i=0; i<n; i++){
        if (isdigit((int)argv[1][i]) == 0){
            printf("Error\n");
            return 1;
        }
        else{
            printf("Success\n");
            printf("%i\n", atoi(argv[1]));   
        }
    }
J...S
  • 5,079
  • 1
  • 20
  • 35
efuzz
  • 117
  • 1
  • 8
  • 1
    You are printing "Success" inside the loop, so it is still just a partial success. – Giacomo Catenazzi May 23 '19 at 14:38
  • 2
    @bruno: (a) `atol` will not do the job of determining whether a string is numeric. It accepts an initial numeric portion and provides no indication of whether that is the entire string or not. (b) Even if it did, it fails to meet a stated requirement: “I need to loop over each…” The goal is not merely to convert a string to a number but to perform an exercise, likely for a class in which in the OP is a student. Calling a library routine fails to teach how to use the control structures being taught. – Eric Postpischil May 23 '19 at 14:40
  • @EricPostpischil I wanted to say **strtol**, is not the first time I do that error damned – bruno May 23 '19 at 14:43
  • You don't want to print "Success" until after you've examined *all* the characters in the string, meaning you want to want until *after* the loop has finished. – John Bode May 23 '19 at 14:44
  • When you say *numeric* are you restricting this to just digits, or are you allowed a prefixed `-`, a decimal separator, and possibly even thousands separators? Then there's scientific notation to worry about. Do please clarify, then it's answerable. – Bathsheba May 23 '19 at 14:48
  • @Bathsheba: “I need to loop over each characters and check if all are digits”. – Eric Postpischil May 23 '19 at 14:54
  • @EricPostpischil: But on the other hand "I need to determine whether or not a user input is numeric." I read the second sentence as an implementation technique. – Bathsheba May 23 '19 at 14:58

1 Answers1

0

If you could use another way, you could use strspn() from string.h.

size_t n = strlen(argv[1]);
if(strspn(argv[1], "0123456789")==n)
{
    printf("Success\n");
}
else
{
    printf("Error\n");
}

strspn() returns the number of bytes in the initial segment of the first argument string which consist only of bytes f rom the second argument string, which in this case are the numbers.

So, if the value returned by strspn() is same as the length of the string, the string has no non-digit characters.


strlen() returns not exactly an int. It is a size_t. It is better to use the latter instead. See strlen to return size_t?.

J...S
  • 5,079
  • 1
  • 20
  • 35