can some please explain what are command line parameters ? i read many books, read online didn't understand anything on what they are and when or how to use them. :(

What exactly did you read, and what exactly didn't you understand? Command line parameters are exceedingly simple, which suggests that you didn't try very hard or are over-complicating things.

Here is another description you can read and maybe not understand. And if that's the case, you need more basic knowledge before you can understand the complexities of command-line parameters.

What exactly did you read, and what exactly didn't you understand? Command line parameters are exceedingly simple, which suggests that you didn't try very hard or are over-complicating things.

sorry i should have made my post more descriptive about what i didn't understood,

sometime user does not want to accept the values at that time of execution of the program then values are passed at the beginning of a program itself.

internally the two parameters 0f the C++ main() function will be used to interpret the values.they are named as arge and argv by convention.
the arge is the first parameter of type int.this supplies the number of commands/words that occurred in the program invocation. if the program is called with two parameters this will have a value of three. this includes the program name in addition to two parameters.

suppose, for example, you execute a c++ program called myapp, as follows
myapp -w file

here, the first command line word is myapp, the name of the program(pointed to by argv[0]. the second word is -w (a program option). the third command-line word is a file name called file.

the argv[] is charecter array holding each parameter value in array. usually the number of the values in the argv[] array is determined using the argc parameter and the values are pulled accordingly.

this is in my class book about command like parameters, i asked my mam to exaplin it but she gave vague answer also this thing doesnt clearly explain about argv, argc and what are command line parameters used for.

now i have to do this activity or assignment:-

modify the existing empsalary module of a telecommunication Inc to increment the salary of their employees. increment amount needs to passed from command.

how can i do this without knowing what command line parameters are.:confused:

Here is another description you can read and maybe not understand. And if that's the case, you need more basic knowledge before you can understand the complexities of command-line parameters.

i am checking it now thx.:)

sorry i should have made my post more descriptive about what i didn't understood,

Yea, you should be clear and concise.

this is in my class book about command like parameters, i asked my mam to exaplin it but she gave vague answer also this thing doesnt clearly explain about argv, argc and what are command line parameters used for.

Yes it does. It's not the greatest explanation, but it is a correct, and accurate description of the parameters.

It is actually your explanation of the problem that is vague. What is it you don't understand? What phrase or explanation is it that confuses you?

1)what is an argument ?

2)

the arge is the first parameter of type int.this supplies the number of commands/words that occurred in the program invocation.

what does supplies the number of commands/words that occurred in the program invocation mean ?
does this mean it will count all the parameters passed in various methods and classes ?

3)does argv[0] store all the parameter values and display them ?

thx.

main() is a function, a somewhat specialized function, but a function, nonetheless. It has a return type, int, a name, main, and a list of arguments contained within ()s, just like any other function in C++. The two arguments for main() are an int, called argc, and an array of C style strings called argv. argc tells the program how many elements there will be in argv. The first element in argv is the name of the program. If you don't submit values for argc and argv then, by default, argc is 1 and argv[0] is the name of the program. If you want to pass more information to main() than just the name of the program that is passed by default, then you need to use the command line. The command line is visulized when you run your program from a command prompt as opposed to within the modified window used by an IDE, such as Microsoft Visual Studio or Borland or whatever. In general terms, if the background on your screen is black and there is a white C with a flashing >, then you are using the command line to pass stuff to main(). If not, you aren't. You can pass large number of strings to main(), though I forget the most you can send. Frequently you enter the information you want so it looks something like this:

C:> myProgramName -c +of gcc w

The information after myProgramName can be extracted from argv and used within your program, just like information passed to other functions can be used within your program. In the case of stuff like the above not only is the string itself important, but the position of each string on the line is important. In your case however, I suspect you will be doing something like:

C:> myProgramName 123

where 123 is argv[1] and, once it is converted from a C style string to an int, it can be used to increment the salary of every person listed in your program.

commented: detailed explanation. +2

main() is a function, a somewhat specialized function, but a function, nonetheless. It has a return type, int, a name, main, and a list of arguments contained within ()s, just like any other function in C++. The two arguments for main() are an int, called argc, and an array of C style strings called argv. argc tells the program how many elements there will be in argv. The first element in argv is the name of the program. If you don't submit values for argc and argv then, by default, argc is 1 and argv[0] is the name of the program. If you want to pass more information to main() than just the name of the program that is passed by default, then you need to use the command line. The command line is visulized when you run your program from a command prompt as opposed to within the modified window used by an IDE, such as Microsoft Visual Studio or Borland or whatever. In general terms, if the background on your screen is black and there is a white C with a flashing >, then you are using the command line to pass stuff to main(). If not, you aren't. You can pass large number of strings to main(), though I forget the most you can send. Frequently you enter the information you want so it looks something like this:

C:> myProgramName -c +of gcc w

The information after myProgramName can be extracted from argv and used within your program, just like information passed to other functions can be used within your program. In the case of stuff like the above not only is the string itself important, but the position of each string on the line is important. In your case however, I suspect you will be doing something like:

C:> myProgramName 123

where 123 is argv[1] and, once it is converted from a C style string to an int, it can be used to increment the salary of every person listed in your program.

ok thx i am going to try it now.

should i write a new program for parsing command line arguments to an existing project? till now i was thinking i could pass them in the existing project main function.

"The most effective way to increase your knowledge is to try new problems in a controlled way. Pick one aspect of C++ that you haven't understood before and write a program that, aside from using that one aspect, uses only things that you have already mastered. Then do what it takes to understand what your program is doing - and why."
- Andrew Koenig and Barabara Moo in 'Ruminations on C++'.

So write a small program; something like this:

#include <iostream>

int main( int argc, char* argv[] )
{
    std::cout << "#arguments (argc): " << argc << '\n' ;
    for( int i=0 ; i<argc ; ++i )
        std::cout << "argument #" << i << " (argv[" << i << "]): " << argv[i] << '\n' ;
}

The run it from the command line with different command lines, look at the output and reason about it. For example, with a program called test:

> ./test
> ./test one two three four
> ./test -one -two=2 three,four

If you are just into arrays, have read something about at, to deepen your understanding write a small program:

#include <iostream>

int main( int argc, char* argv[] )
{
    enum { N = 4 } ;
    short array[N] = { 0, 1, 2, 3 } ;
    std::cout << "#elemebts: " << N << '\n'
              << "size of an element: " << sizeof( array[0] ) << '\n'
              << "size of array: " << sizeof(array) << '\n' ;

    for( int i = 0 ; i < N ; ++i )
         std::cout << "element " << i << "  - address: " << &array[i] << " value: " << array[i] << '\n' ;

    auto p = array ;
    std::cout << "array decays to a pointer. address: " << p << '\n' ;
    ++p ;
    std::cout << "address after increment: " << p << '\n' ;
}

Reason about the output; play around with it - change N from 4 to 7 and run it, change the array of short to an array of double and run it, add some more code which explores some additional aspects and run it. Often, a few lines of code (which you write or reason about) is worth more than a thousand words.

thx for the reply i didnt understood what you mean by this

The run it from the command line with different command lines, look at the output and reason about it. For example, with a program called test:

how do i run it from command line i am using code blocks ?

Build the program from within the CodeBlocks IDE.

Start the command line interpreter (Start Menu -> Run... -> cmd on Windows XP ), switch to the directory which contains the executable (typically <Project Directory>\bin\Debug) and run it from the command prompt.
For example: > test.exe how now brown cow

commented: clear explanation on command line parameters +2

thx for the explanation, now i kinda understood what command lines are.command line parameters are values which are given at the beginning of the program rather than using cin and accepting values when the program is executed. am i correct ?

i am trying to pass the argv[] value to the method display, but it isn't working

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

class empsalary
{
    int salary;
    public:
    void display(int r)
    {
        salary=5000;
        salary=salary+r;
        cout<<salary<<endl;
    }
};
int main(int argc, char* argv[])
{
   char str[]=argv[];
   int result=atoi(str);
   empsalary e;
   e.display(result);
   return 0;
}

Command line arguments are passed to programs automatically when you use them. You just define and use them if you need them, which is almost never done if you are not using a command line based Linux OS.

What you do to run a program on command line, you call it like "myprogram.exe". If you want to run it with arguments like -debugmode, you can add the parameter "myprogram.exe -debugmode".

Then this first argument is assigned to your char** string array.

so i cant pass command line parameters to specific methods ?

Member Avatar for v3ga

Pass command line parameters to main and you can have main pass it to specific methods.
argv is an array of strings. If you execute the program like:
./a.out abc 12
./a.out will be argv[0], abc will be argv[1], 12 will be argv[2] etc.

So in your program, you should have
result =atoi(argv[1])

Pass command line parameters to main and you can have main pass it to specific methods.
argv is an array of strings. If you execute the program like:
./a.out abc 12
./a.out will be argv[0], abc will be argv[1], 12 will be argv[2] etc.

So in your program, you should have
result =atoi(argv[1])

like this ? it is showing some error.

#include <iostream>
#include <stdlib.h>
using namespace std;
class empsalary
{
int salary;
public:
void display(int r)
{
salary=5000;
salary=salary+r;
cout<<salary<<endl;
  }
};
int main(int argc, char* argv[])
{
int result;
result=atoi(argv[1];
empsalary e;
e.display(result);
return 0;
 }

My ability to detect what "some error" is isn't very good. I can't tell if it's becuase there's a missing ) on line 18 or if it's because you sent the wrong set of arguments to main via the command line, whatever.

If "some error" is something displayed by the compiler, what is the error message you get, including what line of code does it reference?

If it compiled and you had run time error(s):
What did you pass on the command line?
What output did you expect?
What output did you get?

My ability to detect what "some error" is isn't very good. I can't tell if it's becuase there's a missing ) on line 18 or if it's because you sent the wrong set of arguments to main via the command line, whatever.

If "some error" is something displayed by the compiler, what is the error message you get, including what line of code does it reference?

If it compiled and you had run time error(s):
What did you pass on the command line?
What output did you expect?
What output did you get?

i was missing ) from line 18 xD, i fixed it and its working. now i fully understand what command line parameters are and how to pass parameters,thx for help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.