Verifying command line parameters on C++ main method

Just a very quick example on how to verify the correct number of command-line parameters on a C++ (or C) main method:
int main(int argc, char **argv) {
if(argc < 3){
...
//give some useful information to the user, i.e. the number
//of required parameters and maybe also a short example
...
//stop the execution of the program
return EXIT_FAILURE;
}
}
The checking of the correct number of parameters is done with the "argc" parameter of the main method, while the actual reading is done by using the argv parameter, i.e. argv[1] for the 1st parameter.
Hint: the input is given as array of char arrays: char**. char* = array of characters = string, so conversion to a string type is straightforward:
string myString = argv[1]
Kindle

Comments

0

Your ad here?