Since all parameters passed to the program are strings, we need a way to convert them to numbers. There are several conversion functions in the standard C++ library that you can use by including <stdlib.h>. Here's a sample of some of the function declarations and example uses:
atoi(const char string[]); // string to int
atol(const char string[]); // string to long
atof(const char string[]); // string to double
int i = atoi("123"); // i is 123
int j = atoi("32.78"); // j is 32
int k = atoi("a"); // k is 0
double d = atof("32.78"); // d is 32.78
This example is add.cpp:
#include <iostream.h> // cout
#include <stdlib.h> // atoi()
void main(int paramCount, char *paramList[])
{
if (paramCount != 3)
{
cout << "Usage: add integer integer" << endl;
return;
}
int operand1 = atoi(paramList[1]);
int operand2 = atoi(paramList[2]);
cout << operand1 + operand2 << endl;
}
Example runs:
add 3 5
8
add 3
Usage: add integer integer