0

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){ 
3
  • 1
    sizeof(*r) is wrong. Note that in c++ sizeof() is a compile time constant. Commented Mar 14, 2020 at 23:11
  • 3
    Apart from what @drescherjm said, you need int check(const char *r){... (note the const) Commented Mar 14, 2020 at 23:19
  • 2
    Read the note for the error message. (Notes go with whatever preceded them.) There is no matching function because the first argument is const char *, but the declared function expects char *, and throwing away const is not allowed. Commented Mar 14, 2020 at 23:38

1 Answer 1

2

char const* to char* is not a valid conversion, you need to remove const from char const *argv[]

int main(int argc, char *argv[]) 

or add const to char *r

int check(const char *r) 

int main(int argc, char *argv[]) 

with

int check(const char *r) 

is also valid.

Also sizeof(*r) is the size of a single char, not the size of the char array you pass as an argument.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.