I am doing a simple c++ program but I have problem when i want to check the command line argument
here is a piece of my code
int main(int argc,char * argv[])
{
if ( argc!=2 && isdigit(argv[1])==0 ) {
cout << "Invalid input for seed \n";
exit(1);
}
what the code do is mainly to check the second argument to see whether it is a digit or not, if it is not, the program must terminate. this doesn't work as the compiler show error message saying
assign1.cpp:15: error: invalid conversion from 'char*' to 'int'
assign1.cpp:15: error: initializing argument 1 of 'int isdigit(int)'
I had a similar problem with the input values earlier today. I'm no expert but i think that argv[] is more like an array of pointers, and each pointer points to an array of characters, if you get what i mean.
Maybe try something like this:
#include <iostream>
#include <locale>
int main(int argc,char *argv[])
{
if ( argc==2 && isdigit(*argv[1]))// Test that there is input and that the input pointed too by argv[1] is a number.
{
//Insert Code Here.
std::cout << "Valid input for seed \n";
}else{
std::cout << "Invalid input for seed \n";
}
return 0;
}