Command Line Arguments
The arguments that you invoke the program with--e.g.: "Lab6.out foo fum"-- are available as parameters to the functionmain()
.
The actual function prototype for main() is:
int main(int argc, char *argv[]);
argc
is the number of command line arguments; argv
is an array of pointers to strings, each pointing to one argument.
The command line has been split at each whitespace, and the name of
the program is the first element in the array. So:
cout << argv[0];will output the name of the program, i.e. "./Lab6.out", and argv[1] will hold "foo", and argv[2] "fum".
argv[argc], i.e. argv[3] in our example, will be a null pointer, i.e., == 0.
Since the arguments are all C-strings, you will have to use the
function atoi()
to convert them to ints for this lab.