I'm trying to error handle command line arguments with a function like this:
int check(char *r){ int arg, valid = 0; for (int i = 0; i<sizeof(*r); i++){ if(!isdigit(r[i])) valid = 0; else if (atoi(r)<1) valid = 0; else valid = 1; } if (valid == 1) arg = atoi(r); while(valid == 0){ cout<<"Input was wrong! Please input a positive integer greater than 0: "; cin>>arg; if (arg<1 || cin.fail()){ cin.clear(); cin.ignore(1000, '\n'); valid = 0; } else valid = 1; } return arg; } int main(int argc, char const *argv[]){ int player; int row; int col; for (int i = 1; i<7; i+=2){ if(argv[i][0] == '-' && argv[i][1] == 'p'){ player = check( argv[i+1] ); } else if (argv[i][0] == '-' && argv[i][1] == 'r'){ row = check( argv[i+1] ); } else if (argv[i][0] == '-' && argv[i][1] == 'c'){ col = check( argv[i+1] ); } else cout<<"Enter -p <number of players> -r <number of rows> -c <number of columns> "<<endl; } } But it won't compile, saying that there was no matching function call to check. An example of what the user should input is "./a.out -p 2 -r 6 -c 7". What is the best way to check if the user is only inputting positive integers in the command line?
The compiler outputs the following message when compiling.
main.cpp:41:15: error: no matching function for call to 'check' player = check( argv[i+1] ); ^~~~~ main.cpp:8:5: note: candidate function not viable: 1st argument ('const char *') would lose const qualifier int check(char *r){ ^ main.cpp:44:12: error: no matching function for call to 'check' row = check( argv[i+1] ); ^~~~~ main.cpp:8:5: note: candidate function not viable: 1st argument ('const char *') would lose const qualifier int check(char *r){ ^ main.cpp:47:12: error: no matching function for call to 'check' col = check( argv[i+1] ); ^~~~~ main.cpp:8:5: note: candidate function not viable: 1st argument ('const char *') would lose const qualifier int check(char *r){
sizeof(*r)is wrong. Note that inc++sizeof() is a compile time constant.int check(const char *r){...(note theconst)const char *, but the declared function expectschar *, and throwing awayconstis not allowed.