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
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:
- How To Pass Ncaa 12?Best solution by espnplayer.com
- How To Pass In Madden?Best solution by Yahoo! Answers
- How To Pass Better In Madden 2012?Best solution by youtube.com
- How To Pass Efficiently In Madden 2012?Best solution by youtube.com
- How To Pass By Sonicwall?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.