Command Line Arguments

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
This tutorial will show you how to eliminate the needs for menus and complex user interactions with your C++ programs. Using the information presented here will allow you to create much simpler input systems for your programs.

If you have ever dealt in the command line interface included with most operating systems, you have probably come across command line arguments. For example, the copy program included in the Windows operating system utilizes command line arguments, such as the following:

Code:
copy hello.txt goodbye.txt
This allows the users of copy to quickly type in a command and have it executed instead of having to go through the traditional dialog of "What file do you want to copy? " and "Where do you want the file to copy to? ". So, you may ask, "how can I get this kind of simplicity in my C++ programs?". Well, C++ makes it easy to include the functionality of command line arguments in your programs.

Lets say we have a simple addition program that takes two numbers and adds them together. First, we will write the code for this program without command line argument functionality. Open up your favorite text editor. Do NOT type in the following code, if you do, you will just have to erase it on the next step. Just look at this code and follow the analysis below:

Code:
#include <iostream>
using namespace std;

int main(void)
{
    int num1, num2;
    
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    cout << "The sum of the two numbers is: " << num1 + num2 << endl;
}
Our program takes two numbers as input and outputs the sum of the two numbers. This program works fine as it is, but if a user is going to add two numbers together, chances are they want to do it quickly. So, here is where the command line argument functionality comes in. Type the following code into your C++ file:

Code:
#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[])
{
    cout << "The sum of the two numbers is: " 
         << atof(argv[1]) + atof(argv[2]) << endl;
}
So, you can see right away that this code looks completely different from the previous code we wrote. The main function declaration should be the first difference you notice. Instead of the traditional int main(void), we have int main(int argc, char *argv[]). What this means is that instead of accepting no arguments, our main function is excepting two arguments. The first argument that the new main statement is excepting is an integer, argc. This argument is the number of arguments that are being sent from the command line to our program. Next comes the character array argv. Argv is the value of the arguments that are being sent to our program. So, if we use the following into the command line and press enter:

Code:
sum 54 87
The value of these two variables would be as follows:
argc = 3
argv = {sum, 54, 87}

Here, you may be seeing something you didn't expect. There are 3 arguments to the sum program instead of just the two numbers we sent to the program. Also, in argv "sum" is one of the arguments. When you send a list of arguments to your program, the name of the program appears as one of the arguments. Unless the name of your program is important to functionality, you can disregard this first argument.

Now, on to the addition part of the program. The code for the addition of the two variables may look a little weird at first, but it is actually simple when you understand it:

Code:
atof(argv[1]) + atof([argv[2])
First of all, lets talk about the data we are using. The two arguments that we are using in the addition are the second and third elements of argv. This is because, as stated above, the program name is one of the arguments, which we will disregard. Next, lets look at the atof functions that we are sending each variable to. The atof function is included in the <stdlib.h> header file that I included just to use this function. It allows us to convert charecter data into floating point data. Because our program naturally accepts charecters as arguments, we have to convert the numbers that were provided before we can add them together. Now, we COULD use the atoi function and convert these numbers to integers, but because we don't know whether the user is going to enter a double or an integer, its best to use atof. One last thing needs to be added to this code before we finish. If you user dosen't know that your program accepts command line arguments and tries to run it, they will end up with a blank console window and an error message. We should make the command line arguments optional, so that users can add numbers whether or not they input a command line argument:

Code:
#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[])
{
    int num1, num2;
    
    if (argc > 1)
    {
        cout << "The sum of the two numbers is: " 
                  << atof(argv[1]) + atof(argv[2]) << endl;
    }

    else
    {
        cout << "Enter the first number: ";
        cin >> num1;
        cout << "Enter the second number: ";
        cin >> num2;
        cout << "The sum of the two numbers is: << " num1 + num2 << endl;
    }
}
Now if a user runs your program without providing command line arguments, they will be greated with the traditional prompt dialog.

One more word about command line arguments before I finish. Like all input, command line arguments need to be validated for accuracy before taking any action. For breverity and because validation is really outside the scope of this tutorial, I have left out any validation code. If you are taking command line arguments in your program, you should also account for excess or not enough arguments, which I did not do here (for breverity). This can be accomplished with if statements or error handling techniques.

This code was written and compiled in Dev-C++ and may need some tweaking to run in other compilers. The source code used in this project is included for convenience. If you would like to contribute to the information presented here, or offer corrections, please post a reply.

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top