Results 1 to 8 of 8

Thread: Program arguments

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Program arguments

    How can I pass arguments to a console program? Something like: progname -a
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Ok, that helped a lot. I wrote a little program using it, but it crashes.

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main(int argc, char* argv[])
    {
    	for (int i = 1; i <= argc; i++)
    	{
    		if (strcmp(argv[i], "-a") == 0)
    			cout<<"ok"<<endl;
    	}
    	return 0;
    }
    If i type "<progname> -a" it prints ok, but it still crashes.
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    How does it crash? Does it ever get to the return 0?

  5. #5
    Zaei
    Guest
    Dont know if this is the problem, but change your for loop to this:
    Code:
    for(int i = 0; i < argc; ++i) {.. }
    Z.

  6. #6
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    USe
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main(int argc, char* argv[])
    {
    	for (int i = 1; i < argc; i++)
    	{
    		if (strcmp(argv[i], "-a") == 0)
    			cout<<"ok"<<endl;
    	}
    	return 0;
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  7. #7
    Zaei
    Guest
    I win =P.

    Z.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Also remember that argv[argc] is defined (in the standard) to be NULL
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width