Page 1 of 1
Command Line Arguments in C++
Posted: Wed Feb 18, 2009 7:46 pm
by herby490
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.
Re: Command Line Arguments in C++
Posted: Wed Feb 18, 2009 8:56 pm
by wtetzner
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:
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;
}
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
Re: Command Line Arguments in C++
Posted: Thu Feb 19, 2009 8:09 am
by PixelP
thanks, i didnt know that. :P
Re: Command Line Arguments in C++
Posted: Thu Feb 19, 2009 8:28 pm
by herby490
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.
Re: Command Line Arguments in C++
Posted: Thu Feb 19, 2009 8:30 pm
by Ginto8
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.
uhh... are you linux or windows?
cuz if you're on windows, you have to do "start [filepath] [arguments]"; arguments are optional.
Re: Command Line Arguments in C++
Posted: Thu Feb 19, 2009 8:36 pm
by wtetzner
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.
OK, when you open the command prompt, type: >cd [Path to folder containing executable file]
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.
Re: Command Line Arguments in C++
Posted: Fri Feb 20, 2009 6:15 pm
by herby490
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
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;
}
I have been able to compile it just not run it from the command prompt
Re: Command Line Arguments in C++
Posted: Sat Feb 21, 2009 2:44 am
by wtetzner
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?
Re: Command Line Arguments in C++
Posted: Sat Feb 21, 2009 9:41 pm
by herby490
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"
Re: Command Line Arguments in C++
Posted: Sun Feb 22, 2009 1:44 am
by wtetzner
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
Re: Command Line Arguments in C++
Posted: Sun Feb 22, 2009 10:00 am
by herby490
Thanks i finally get how to use it.