Command Line Arguments in C++
Moderator: Coders of Rage
Command Line Arguments in C++
I'm learning c++ and i came across these Command Line Arguments and I dont have the slightest idea of what their saying. I have copied some source code from the book and I have tried compiling it but I dont get the results the book says i should get. Could someone explain these for me.
- wtetzner
- Chaos Rift Regular
- Posts: 159
- Joined: Wed Feb 18, 2009 6:43 pm
- Current Project: waterbear, GBA game + editor
- Favorite Gaming Platforms: Game Boy Advance
- Programming Language of Choice: OCaml
- Location: TX
- Contact:
Re: Command Line Arguments in C++
Command line arguments are the parameters you pass in to your program when running it from a command prompt.
For example, if you program executable file is a.out, you would call it like this: $./a.out
If you want to pass an argument to it, say your name, you could do this: $./a.out myname
where myname is your name.
Or if you're using windows, your program name might be something like test.exe.
So to run it you would type this at the command prompt: >test.exe
If you want to pass your name to it: >test.exe myname
Assuming you main() function looks something like this:
The parameters are for handling command line arguments. The number of arguments that are passed to your program is stored in argc. The arguments are stored as char arrays in argv. The name of the program is always passed in at argv[0]. So the first parameter after the program name would be stored at argv[1].
Hopefully I was clear enough.
-Walter
For example, if you program executable file is a.out, you would call it like this: $./a.out
If you want to pass an argument to it, say your name, you could do this: $./a.out myname
where myname is your name.
Or if you're using windows, your program name might be something like test.exe.
So to run it you would type this at the command prompt: >test.exe
If you want to pass your name to it: >test.exe myname
Assuming you main() function looks something like this:
Code: Select all
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
char* name = "World"; // Default word to print
if(argc > 1) name = argv[1]; // If there is a name argument, use that
// (first argument is always the program name)
cout << "Hello " << name << "!" << endl;
return 0;
}
Hopefully I was clear enough.
-Walter
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
- PixelP
- Chaos Rift Regular
- Posts: 153
- Joined: Tue Oct 07, 2008 12:23 pm
- Programming Language of Choice: c/c++
- Location: sweden
- Contact:
Re: Command Line Arguments in C++
thanks, i didnt know that. :P
Re: Command Line Arguments in C++
Ok i get the part about how you call the program through the command prompt now but i'm having trouble calling the function. When i try it i get test.exe is not a recognizable command.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Command Line Arguments in C++
uhh... are you linux or windows?herby490 wrote:Ok i get the part about how you call the program through the command prompt now but i'm having trouble calling the function. When i try it i get test.exe is not a recognizable command.
cuz if you're on windows, you have to do "start [filepath] [arguments]"; arguments are optional.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
- wtetzner
- Chaos Rift Regular
- Posts: 159
- Joined: Wed Feb 18, 2009 6:43 pm
- Current Project: waterbear, GBA game + editor
- Favorite Gaming Platforms: Game Boy Advance
- Programming Language of Choice: OCaml
- Location: TX
- Contact:
Re: Command Line Arguments in C++
OK, when you open the command prompt, type: >cd [Path to folder containing executable file]Ok i get the part about how you call the program through the command prompt now but i'm having trouble calling the function. When i try it i get test.exe is not a recognizable command.
and press enter.
then type: >test.exe [arguments]
Oh, and make sure you're using the actual name of your executable. If it's something other than test.exe, use that instead.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
Re: Command Line Arguments in C++
Ok i did that but now i am getting a syntax error when i try to run it.
Here is the code that i am compiling
I have been able to compile it just not run it from the command prompt
Here is the code that i am compiling
Code: Select all
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int a;
for(a=0;a< argc; a ++)
{
cout << argv[a] << endl;
}
return 0;
}
- wtetzner
- Chaos Rift Regular
- Posts: 159
- Joined: Wed Feb 18, 2009 6:43 pm
- Current Project: waterbear, GBA game + editor
- Favorite Gaming Platforms: Game Boy Advance
- Programming Language of Choice: OCaml
- Location: TX
- Contact:
Re: Command Line Arguments in C++
Hmm, I just compiled your code and it runs perfectly. What exactly are you doing when you run it from the command line? And what error are you getting?
Could you copy and paste the output after running the program from the prompt?
Could you copy and paste the output after running the program from the prompt?
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
Re: Command Line Arguments in C++
When i compile the code normally it shows the path to where the file is. When i try to run it from the command prompt i get "The syntax of the command is incorrect"
- wtetzner
- Chaos Rift Regular
- Posts: 159
- Joined: Wed Feb 18, 2009 6:43 pm
- Current Project: waterbear, GBA game + editor
- Favorite Gaming Platforms: Game Boy Advance
- Programming Language of Choice: OCaml
- Location: TX
- Contact:
Re: Command Line Arguments in C++
Haha, I just realized you might be actually typing the ">" in ">test.exe".
The ">" is just supposed to represent the prompt.
Just type: test.exe
to run the program.
Sorry about that.
-Walter
The ">" is just supposed to represent the prompt.
Just type: test.exe
to run the program.
Sorry about that.
-Walter
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
Re: Command Line Arguments in C++
Thanks i finally get how to use it.