Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops.

Verifying command line parameters on C++ main method

1 min read

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]
comments powered by Disqus