Reading input from the command line is a bit mysterious to beginners, and hard to extract from reference manuals. The declarations and commands will eventually make sense to you. But at first you may want to treat them as magic, and copy them with minor adjustments.
The main function takes two arguments. Well, actually it takes three, but only insane hackers know about the third one. The main function can be written as if it took two arguments. Or you can pretend it takes no inputs, if you don't intend to use them.
main () // version ignoring the inputs { .... main(int argc,char *argv[]) // version including the inputs {....
The input argc is the number of items on the command line. C++ divides the command line into items by breaking it at whitespace (e.g. spaces). The items include the command name itself, so
The input argv is an array of strings, one string per item. So argv[0] is the command name, argv[1] is the first input, etc.
Numbers on the command line appear in argv as strings, not as numbers. That is, they are still raw sequences of characters: digits, decimal points, etc. To translate them into numbers, use the functions atoi and atof.
If you use atoi and atof, your program must include the stdlib header file:
#include <stdlib.h>
You may find it helpful to copy the file simple.cc. This contains a short example program which reads an integer from the command line. You can see how this program works and then modify it to suit your purposes.
This page is maintained by Geoff Kuenning.