salesTax salesTaxRates TX 3.45 3.45 123.45into any shell. Here,
(3.45 + 3.45 + 123.45)*6.25% = 8.146875,which is then rounded to the closest penny.
The example given above has six command-line arguments, separated by whitespace. Just as for arrays, they are numbered starting with zero. For example, argv[2] is TX, and argv[5] is 123.45.
Here are the three rules for using command-line arguments:
For an example of a program that uses command-line arguments, see echo1perline.cc.
All command-line arguments are C-style strings, even for numeric arguments; e.g., argv[3] is the string ``3.45''. Tip: Use the strtod() library function to convert from a string to a double. Under Linux/Unix, you can get online help about this function by typing man strtod or info strtod. For example, use
strtod(argv[3], static_cast<char **>(0))to convert the string to its double value 3.45. Be sure to include stdlib.h.