Results 1 to 14 of 14
  1. #1
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah

    command-line arguments

    the module 5 mastery check said:

    8. Write a program that requires a password that is specified on the command line. Your program doesn’t have to actually do anything except report whether the password was entered correctly or incorrectly.
    i forget how it worked so i looked back. after reading that part of the chapter again i still didn't understand it so i made the example program to make it more clear. but the program didn't work. and the last system pause is never executed.

    1 can someone explain to me how command-line arguments work and what they do?

    2 can someone tell me why the code doesn't work.

    the code of the example program that doesn't work:
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        double a, b;
        if (argc!=3)
        {
                    cout <<"usage: add num num" <<endl;
                    system("pause");// added this one so the screen wouldn't disappear
                    return 1;
        }
        
        a = atof(argv[1]);
        b = atof(argv[2]);
        
        cout  << a+b;
        
        system("pause");
        return 0;
    }
    Last edited by lalakijilp; 10-20-2009 at 10:01 AM.

  2. #2
    Lynie's Avatar
    Join Date
    Jun 2008
    Gender
    male
    Posts
    27
    Reputation
    10
    Thanks
    2
    Google "How C Programming works" and go the 39th chapter. And don't use System("pause").

  3. #3
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    Quote Originally Posted by Lynie View Post
    Google "How C Programming works" and go the 39th chapter. And don't use System("pause").
    if i dont use system pause the program will execute and close in a milisec and i will only see a flash...

  4. #4
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    Your code works just fine, but you may want to add a '<< endl;' where you print out the numbers.

    It's really not that hard;
    ARGC has how many arguments there are INCLUDING the exe name.
    ARGV holds the strings for these arguments

    So if you have 'Add.exe 3 5.4' you'll get
    Argc = 3;
    and Argv[0] = "Add.exe"
    Argv[1] = "3";
    Argv[2] = "5.4";

    Lynie and a lot of other people hate System("Pause") and would rather have you use getch() or something similar. They have a point -See my post here. Though it seems silly to keep rehashing that same point over and over to people who are just learning. If you have something that works and makes sense to you go with it.. not like any of this is 'commercial grade' software
    Last edited by B1ackAnge1; 10-20-2009 at 10:28 AM.

  5. #5
    Lynie's Avatar
    Join Date
    Jun 2008
    Gender
    male
    Posts
    27
    Reputation
    10
    Thanks
    2
    Quote Originally Posted by lalakijilp View Post
    if i dont use system pause the program will execute and close in a milisec and i will only see a flash...
    Then use another way to wait for key input, such as getchar()

  6. #6
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    Quote Originally Posted by B1ackAnge1 View Post
    Your code works just fine, but you may want to add a '<< endl;' where you print out the numbers.

    It's really not that hard;
    ARGC has how many arguments there are INCLUDING the exe name.
    ARGV holds the strings for these arguments

    So if you have 'Add.exe 3 5.4' you'll get
    Argc = 3;
    and Argv[0] = "Add.exe"
    Argv[1] = "3";
    Argv[2] = "5.4";

    Lynie and a lot of other people hate System("Pause") and would rather have you use getch() or something similar. They have a point -See my post here. Though it seems silly to keep rehashing that same point over and over to people who are just learning. If you have something that works and makes sense to you go with it.. not like any of this is 'commercial grade' software
    thanks but....

    the code probably is right but the program doesnt work.
    it looks like the program never gets past the if

  7. #7
    Lynie's Avatar
    Join Date
    Jun 2008
    Gender
    male
    Posts
    27
    Reputation
    10
    Thanks
    2
    Quote Originally Posted by lalakijilp View Post
    thanks but....

    the code probably is right but the program doesnt work.
    it looks like the program never gets past the if
    Could you write the output of your program?

  8. #8
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    I ran it and it worked.
    What are you passing to the command line? If you're running it from within Visual Studio, then you'll have to set the arguments in the project properties.
    Right click on the project->Properties,
    Then go to "Debug" and you'll see an entry for 'Command Line Arguments"
    Where then you'd type : 3 5.4
    or whatever numbers you want to pass in

    Or you could simply open a dos prompt in the folder where you have your EXE and run it from there (which also negates the need for any of this pause/getch stuff)

  9. #9
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    Quote Originally Posted by B1ackAnge1 View Post
    I ran it and it worked.
    What are you passing to the command line? If you're running it from within Visual Studio, then you'll have to set the arguments in the project properties.
    Right click on the project->Properties,
    Then go to "Debug" and you'll see an entry for 'Command Line Arguments"
    Where then you'd type : 3 5.4
    or whatever numbers you want to pass in

    Or you could simply open a dos prompt in the folder where you have your EXE and run it from there (which also negates the need for any of this pause/getch stuff)
    i use dev c++ and i didn't "pass" anything to the command line

    and how do you pass anything to the command line?
    because that is the opdracht en het is niet uitgelegt in de tutorial

  10. #10
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    yeah.. what's happening is that it's running the app without anything on the command line so it always gets a argc of 1.

    just googled really quickly:
    "To pass command-line parameters to your program, go to the "Execute" menu, choose "Parameters" and type in any paramaters you wish to pass."

    So there you'd type 2 numbers


    or just open a command prompt, cd to the drive\folder where your exe sits and just type it in old-school
    add.exe 3 5.4

    But most people nowadays don't know how to use a command prompt

  11. #11
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    create a shortcut to the exe and put it behind the path?
    Ah we-a blaze the fyah, make it bun dem!

  12. The Following User Says Thank You to Hell_Demon For This Useful Post:

    lalakijilp (10-20-2009)

  13. #12
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    it works but only if it is executed with dev C++
    why is this happening

  14. #13
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    If you run it by just double clicking you're again not passing in any arguments thus it will find argc == 1

    you have to pass the arguments to it by either
    a) Passing them through your Dev environment
    b) Do what HD said and make a shortcut and add them there
    c) Run the program from the Dos-Prompt (which you should learn since
    most of these apps you're writing are really made to be ran like that anyway).

  15. The Following User Says Thank You to B1ackAnge1 For This Useful Post:

    lalakijilp (10-20-2009)

  16. #14
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    thanks it worked

Similar Threads

  1. [Tutorial] Interfacing with Command-Line programs.
    By Jason in forum Visual Basic Programming
    Replies: 4
    Last Post: 08-29-2011, 08:46 PM
  2. Command line parameters
    By m0k1 in forum Combat Arms Coding Help & Discussion
    Replies: 0
    Last Post: 08-19-2011, 05:45 AM
  3. Starting Minecraft Command-Line!
    By hexacyanide in forum Minecraft Discussions
    Replies: 2
    Last Post: 08-09-2011, 04:30 AM
  4. [Help] Start a program with command line args [solved]
    By Kudi in forum Visual Basic Programming
    Replies: 3
    Last Post: 07-16-2011, 07:29 AM
  5. [Tutorial] Reading from the CMD line
    By shercipher in forum C++/C Programming
    Replies: 7
    Last Post: 04-04-2006, 12:49 PM

Tags for this Thread