Saturday, October 19, 2013

Adding 2 numbers in Command line arguments

Program:

//ADD.CPP
#include<iostream.h>
#include<conio.h>                    
#include<stdlib.h>                    
void main(int argc , char * argv[])
{
    clrscr();
    int i,sum=0;
    if(argc!=3)
    {
      cout<<"\nYou have forget to type numbers";
      getch();
      exit(1);
    }
    cout<<"\nThe SUM is:";
    for(i=1;i<argc;i++)
    sum = sum + atoi(argv[i]);
    cout<<sum;
    getch();
}

Output:

[Ctrl+F9 does'nt work here]
Navigate File ---> DOS shell --->Type Program name . extention (ie. ADD.CPP 20 30)

ADD.CPP 20 30
The SUM is:50

No comments: