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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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 actuallyyour explanation of the problem that is vague. What is it you don't understand? What phrase or explanation is it that confuses you?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
"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.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
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 \bin\Debug) and run it from the command prompt.
For example: > test.exe how now brown cow
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
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])
v3ga
Junior Poster in Training
95 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
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?
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396