How to pass char** in python ctype?

Confused on how to pass char *argv[]?

  • Im need to check if argv[1] is a number but im not sure how to pas it to a function to check with is digit. This is what I have right now. bool is_legal(char *argv[]) { for(int i = 0; argv[i]; i++) if(!isdigit(argv[i])) { cerr << "Error: invalid number of checkers specified."; return 1; } return 0; } and im passing is_legal(argv[1]);

  • Answer:

    If you are simply passing one variable within the array index, then you function implementation "is_legal" doesnt need the * "argv" is a 2-D array of chars, but when you declare your function declaration like this "is_legal(argv[1]);" ^ you are now passing a one dimensional array to a function, because "argv[1]" is only one index within that array So your "is_legal" function should look like this __ bool is_legal(char argv[]) { int i=0; while(argv[i]!='\0') { if(!isdigit(argv[i])) { cerr << "Error: invalid number of checkers specified."; return 1; } ++i; } return 0; }

Nicolas at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

isdigit operates on unsigned chars cast to ints. You want to write a function that operates on strings. #include <cctype> bool numeric(const char *s) {       while (*s) {             if (!isdigit((unsigned char) *s++))                   return 0;       }       return 1; } EDIT: Since your function takes a predicate form, it's natural to return true (non-zero) when the predicate evaluates to true and false (zero) when it doesn't. Since an entirely numerical set of strings is what you want, then you ought to switch your return values around.

henni

Do you want to check argv[1] contains only digits or not. If so, bool is_legal(char *argv[]) { for(int i = 0; argv[1][i]!='\0'; i++) <-----------------------see 2D represemt if(!isdigit(argv[1][i])) <- see 2D notation { cerr << "Error: invalid number of checkers specified."; return 0 ; } return 1; } the above function checks whether argv[1] contains digits only or not. If it does not it returns 0 else it returns 1.

James Bond

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.