Setting the overall goal aside for the time being, what it sounds like is that you want help in processing command-line arguments. While the details of this can be rather tricky, the basics are fairly straightforward.
In C and C++, command-line arguments are passed to the program as a pair of optional parameters for the main()
function. These parameters are an int
value and an array of array of char
. By convention, these are named argc
(for 'argument count') and argv
(for 'argument values'). The full function header of main()
thus becomes:
int main(int argc, char* argv[])
Generally, you would start by checking the value of argc
to see if any arguments were passed, and if so, how many. You would then check the values in the individual sub-arrays of argv
to get the actual arguments. By convention, under most systems, the zeroth argument is the path of the executable program itself.
To give a simple example, here is a simple program that echos the arguments to stdout:
#include <iostream>
#include <cstdlib>
int main(int argc, char* argv[])
{
if (argc < 1)
{
std::cerr << argv[0] << ": no arguments passed" << endl;
exit(-1);
}
else
{
for (int i = 1; i <= argc; i++)
{
std::cout << argv[i];
}
}
return 0;
}
Now, this doesn't really process the arguments much; it accepts no flags or switches, and doesn't actually use the values of the arguments other than to repeat them back out. …