CommandLine

Hey!Guys.I have read on passing commandLine arguments in main() function,about 3 times,but i got nothing,i only know its syntax but,not to write program the make sense about commandLine,so please give me example and how it work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main( int argc, char *argv[] ) // argc = number of arguments, argv = arguments as array of C strings
{
    std::cout << "The name of the executable file of this program is " << argv[0] << '\n'; // argv[0] is always the name of the executable file
    
    if ( argc > 1 ) // check if there are some arguments, when no arguments were given argc==1 ( there is at least argv[0] )
    {
        std::cout << "The arguments you passed are:";
        for ( int i  = 1; i < argc; i++ )
            std::cout << "\n\t" << argv[i]; // print out all the arguments
    }
    else
       std::cout << "You didn't pass any argument :-(";

    std::cout << std::endl;

    return 0;
}
Last edited on
Wow!Thanks.But how to pass more then filename,i recall from DOS there is command 'Exit' how my program can have like those commandLine.Our they are difference from what we talk here!
how to pass more than filename?
program.exe arg1 arg2 arg3
example:
"frozen throne.exe" -windows
this lets you play warcraft in window mode. half life also do this to jump directly to counter strike mode

how my program can have like those commandLine?
something like this:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
    string icmd = "";
    getline(cin, icmd);
    if(icmd.compare("exit")==0)
        return 0;
    else
        cout << "doing some actions.";
    return 0;
}


yes they are different. look at my example
Thank,guy.Now i get it!But i think i need some challenges and more example.Do you have any site which have them?
One thing blackcoder.I'm months begginer so srry for my childhood questions.U can tell me that,now i can run my program from command prompt,by type it name!? Or run at certain block of code?
Thank,guy.Now i get it!But i think i need some challenges and more example.Do you have any site which have them?

what do you mean? PM me on this topic.

One thing blackcoder.I'm months begginer so srry for my childhood questions.U can tell me that,now i can run my program from command prompt,by type it name!? Or run at certain block of code?

yes you can run your program in the command prompt or in the exe shortcut. if you have the game warcraft 3 game or half life try researching and making it work in window mode. i think experience will satisfy you more.

and yeah i'm also months beginner. i think we can be friends PM me ok
Topic archived. No new replies allowed.