Friday, January 4, 2013

How to write C++ programs using command line arguments

Before moving to the code, you have to know what are these command line arguments.

Have you ever noticed In many C++ IDE's and compilers, when it generates the main function for you, it looks like this:
    int main(int argc, char *argv[])

When we code C++ without an IDE, just with a command line compiler,we type (what you did up to now is)
     int main()

without any parameters. what do you think these argc and argv are used for? Yes. If you are writing a c++ program which requires command line arguments, you must go with the method signature of main method that is shown above along with argc and argv parameters. 

argc - is an integer variable. It gives you the number of command line arguments that you are entering.
argv- is a character array. this gives you an array of command line arguments that you input in your program. you can access each argument as argv[1],argv[2] ...

Note that we have to skip the first argument argv[0] because when running the program, the first argument is always the name of the executable file.

Let me explain this with a simple console application

#include<cstdlib>                    //atoi and atof are available in this library
using namespace std;
int main(int argc,char* argv[])   //we expect this program to have command line arguments
char name[20];                //we need this to store a name with maximum of 20 characters
int age;
double salary;

if(argc<4 || argc>4)         //if the number of arguments entered is not exactly 3
cout<<"Number of arguments should be 3"<<endl; //display this message and end the program

else                                //if user entered exactly three arguments
strcpy(name,argv[1]); //copy the first argument argv[1] to name array
age=atoi(argv[2]);     //convert the second argument argv[2] to integer and assign it to age variable
salary=atof(argv[3]); //convert the third argument argv[3] to float and assign it to salary variable

cout<<"Name is:"<<name<<endl; //display the name
cout<<"Age is:"<<age<<endl; //display the age
cout<<"Salary is:"<<salary<<endl;//display the salary

return 0;

atoi(), satol(), atoll() functions

These functions are defined in header <cstdlib> 

int atoi( const char *str ); 
flaot atof( const char *str ); 
long atol( const char *str ) 
long long atoll( const char *str ); 

Interprets an integer value in a byte string pointed to by str. Function discards any white space characters until first non-white space character is found. Then it takes as many characters as possible to form a valid integer number representation and converts them to integer value. The valid integer value consists of the following parts:
(optional) plus or minus sign
  • numeric digits
  • Parameters

str - pointer to the null-terminated byte string to be interpreted

Return value

Integer value corresponding to the contents of str on success. If the converted value falls out of range of 

output of the above program:-