Programming Forums
User Name Password Register

RSS Feed

Thread Tools Display Modes
Old Mar 23rd, 2009, 9:03 PM   #1
Professional Programmer
Join Date: Nov 2008
Posts: 336
Rep Power: 10 lrh9 is on a distinguished road
C++ - Creating programs that utilize command line arguments.

This tutorial will teach you how to create programs that use command line arguments.

Terms to know:

1) Argument - A value that is to be used in a function.

2) Command line arguments - Arguments to be used in the "main()" function. Users type these arguments when calling a program via the command line interface. They are typed after the program name.

You've probably used arguments in your C++ programs. Many functions you create in your programs utilize arguments.

Here is an example of a program that uses arguments.

C++ Syntax (Toggle Plain Text)
  1. /*
  2. This program is intended to demonstrate function arguments.
  3. It may not factorial integers greater than nine.
  4. */
  6. #include <iostream>
  7. using namespace std;
  9. /*
  10. When the function is called, the value of the argument it
  11. is called with will be stored in nNumber.
  12. */
  14. int factorial(int nNumber)
  15. {
  16. for(int nIterator = nNumber - 1; nIterator > 0; nIterator--)
  17. {
  18. nNumber *= nIterator;
  19. }
  21. return nNumber;
  22. }
  24. int main()
  25. {
  26. int nUserInput;
  28. cout << "To find n!, enter n.\n";
  30. cin >> nUserInput;
  32. cout << nUserInput << "! is equal to ";
  34. /*
  35. The following line calls the factorial function with the
  36. argument (value to be used in the function) of nUserInput.
  37. */
  38. nUserInput = factorial(nUserInput);
  40. cout << nUserInput <<".\n";
  42. return 0;
  43. }

You only need to learn a few more rules to begin utilizing command line arguments.

1) Command line arguments are passed to the "main()" function when executing the program from the command line. They are typed after the program name.

2) Command line arguments are stored as an array of strings. In order to use non-string values as command line arguments, you will need to write code to convert the string argument to its intended numerical value.

3) The standard form of coding command line arguments is, "int main(int argc, char * argv[])". "argc", and "argv" can be named any name that would be valid for a variable name, but "argc", and "argv" are the standard names. "argc" means argument counter. It is the number of arguments passed to the main function. "argv" means argument value. It is the value stored in the argument.

Note: Another form of coding command line arguments is, "int main(int argc, char ** argv[])". The code with the single asterisk and the double asterisk perform the same function.

4) The program name is automatically "argv[0]". User arguments start at "argv[1]".

5) Arguments are separated by spaces.

Here is an example program that utilizes command line arguments. It will display the arguments the user types to the console.

To execute it, open your command line interface. (Usually cmd.exe in Windows. It can be opened using "Run..." on the Start menu.)

Change the working directory to the location of the built program, or copy the built program to the working directory. (On Windows, you can change the working director to a partition by typing "cd " without the quotes and the name of the partition. Usually it is "C:\". Example: "cd C:\" without the quotes.)

Type the name of the program, then type the words you wish to display.

Example: "program.exe Hello, world!" without the quotes.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  4. int main(int argc, char * argv[])
  5. {
  6. for(int nIterator = 1; nIterator < argc; nIterator++)
  7. {
  8. /*
  9. The following line is what actually uses the command line
  10. arguments. To reference a specific argument, you use the
  11. index of the argument. Remember, argument zero is always
  12. the name of the program, and each subsequent argument is
  13. separated by a space. So typing "program.exe Hello, world!"
  14. in the command line would create 3 arguments. Argument zero
  15. is "program.exe". Argument one is "Hello,", and argument two
  16. is "world!". Spaces aren't passed as arguments.
  17. */
  18. cout << argv[nIterator];
  19. if(nIterator < argc - 1)
  20. {
  21. cout << " ";
  22. }
  23. /*
  24. Remember, spaces aren't passed as arguments. In this
  25. program, they need to be included with code.
  26. */
  27. }
  29. cout << "\n";
  31. return 0;
  32. }

The output for the arguments "Hello," and "world!" should be "Hello, world!".

Now you know how to use command line arguments.
lrh9 is offline   Reply With Quote
Old Mar 23rd, 2009, 11:21 PM   #2
hoßßy¦st C++ Þrogrammer
Join Date: Sep 2008
Posts: 816
Rep Power: 10 cdingSpree will become famous soon enough
Send a message via Skype™ to cdingSpree
Re: C++ - Creating programs that utilize command line arguments.

For Win32, there is a LPSTR (which is a char*) in place.
C++ Syntax (Toggle Plain Text)
  1. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  2. LPSTR lpCmdLine, int nCmdShow)
cdingSpree is offline   Reply With Quote


« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating programs that can speak PhilBon Visual Basic .NET 2 Sep 10th, 2007 10:04 AM
Creating Windows Services/LocalSystem Programs Baphomet C++ 1 Oct 19th, 2006 1:39 PM
the best way to pass data between two programs nindoja C# 7 Dec 27th, 2005 12:54 AM
Creating a spyware removal app pizzaboy14x Other Programming Languages 11 Sep 27th, 2005 9:48 PM
Comparing arguments to strings layer C++ 2 May 7th, 2005 3:39 PM

DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:39 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC