Question

I have the following array:

array[];

I take users input as "cd SOMETHING"The "SOMETHING", should be a number. BUT if the user enters "cd Hello" I want to give an error message. Here is the code that i am using to split the user input:

char *arguments[80];
char *split;

split = strtok(userInput," ");

while(split != NULL)
{
    arguments[count++] = split; 
    split = strtok(NULL, " ");
}

Now i have the usersInput stored within my arguments array as:

arguments[0]; (cd) and arguments[1]; (Hello)

Now before I try and pass the arguments to my next function (which needs a number) I need to check if arguments[1] is a number or the crap that is currently is. Then if it is not a number, display an error message, and if it is a number, I can proceed as I would like to.

I tried both isalpha() and isdigit() but they only work for a single char.

I understand that I could loop through argument[1] and split it into individual chars, then loop through those and check each one with isalpha or isdigit but this is somewhat of a hassle, I would like to know if there is a easier way to do what I am trying to do?

Était-ce utile?

La solution 2

You could also use strtol (if the input is supposed to be an integer, anyway):

#include <errno.h>
...
char *chk = NULL;
errno = 0;
long val = strtol( arguments[1], &chk, 0 );
if ( !isspace( *chk ) && *chk != 0 )
{
  fprintf( stderr, "%s is not a valid integer string!\n", arguments[1] );
}
else
{
  if ( errno == ERANGE ) 
  {
    fprintf( stderr, "%s cannot be represented as a long\n" );
  }
  else
  {
    // arguments[1] is a valid integer string
    do_something_with( val );
  }
}

strtol takes the input string and attempts to convert it to the corresponding integer value (result is a long). chk will point to the first character in the string that is not part of a valid integer string; if this character is not whitespace or 0, then the input was not a properly formed integer string. The last argument 0 indicates that the input string can be in either hex, decimal, or octal formats, and will be converted appropriately. If you want to limit valid inputs to decimal format, use 10.

Autres conseils

I understand that I could loop through argument[1] and split it into individual chars, then loop through those and check each one with isalpha or isdigit but this is somewhat of a hassle

Hassle or not, it is probably the easiest way to do it in C while allowing long arguments.

int looks_like_number(const char *p)
{
    while (*p) {
        if (!isdigit(*p))
            return 0;

        ++p;
    }
    return 1;
}

Or you can variate and say:

while (*p && isdigit(*p)) {
    ++p;
}

return *p == 0;

You don't actually need to split up the string into characters since you can simply iterate over them.

while(split != NULL)
{
    arguments[count++] = split;

    while(isdigit(*split)) ++split;
    if(*split) error("non-numeric string!");

    split = strtok(NULL, " ");
}

This increments split until a non-digit character is found; if it's not the null character terminating the string then you generate an error.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top