Command Line Arguments in C++

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Command Line Arguments in C++

Post 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.
User avatar
wtetzner
Chaos Rift Regular
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++

Post 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
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.
User avatar
PixelP
Chaos Rift Regular
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++

Post by PixelP »

thanks, i didnt know that. :P
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: Command Line Arguments in C++

Post 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.
User avatar
Ginto8
ES Beta Backer
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++

Post 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. ;)
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.
User avatar
wtetzner
Chaos Rift Regular
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++

Post 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.
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.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: Command Line Arguments in C++

Post 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
User avatar
wtetzner
Chaos Rift Regular
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++

Post 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?
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.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: Command Line Arguments in C++

Post 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"
User avatar
wtetzner
Chaos Rift Regular
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++

Post 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
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.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: Command Line Arguments in C++

Post by herby490 »

Thanks i finally get how to use it.
Post Reply