Thread: arguments

  1. #1


    This may seem simple but i cant figure it out:
    I want to be able to pass a single argument to my program and i can do this, i can also get it to print to stdout but when i try to use an 'if' statment with argv[1] it just goes to my else even if it matches with the if.
    an example similar to my basic prog:

    int main(int argc, char *argv[])
    cout<<argv[1]<<endl; //this prints out "foo"
    //assuming i used arg "foo"

    if(argc !=2)
    return 0;

    else if(argv[1] == "foo")
    return 0;

    cout<<"no argument\n"; //this is what prints
    //even if i use "foo"
    //as an argument
    return 0;
    sorry if this seems too basic but i am getting a headache trying to figure it out. What am i doing incorrectly?


  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002

    Re: arguments

    Originally posted by mungyun
    This may seem simple but i cant figure it out:
    else if(argv[1] == "foo")
    When you want to compare strings, use strcmp or strncmp (man pages available for both). Your 'else if' should be:
    else if ( strcmp(argv[1], "foo") == 0 )
    Hope that helps,
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM