Thread: processing command line args

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    processing command line args

    Here is my program:
    Code:
    int main( int argc, char *argv[] ){
    
        if( argc == 2 ){
    
            cout << "Found an argument." << endl;
    
            cout << argv[1] << endl; // Displays 'auto' to the screen.
    
            if( argv[1] == "auto" ){ // This statement is NOT true...
    
                cout << "Found 'auto' argument." << endl;
            }
        }
    
        return 0;
    }
    The argument is found no problem, but the second if statement is NOT true. Why? Do I need to overload the operator==?

    Help please.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can either use strcmp(), or use std::string, e.g.,

    Code:
    if (std::string(argv[1]) == "auto")
    At the moment the code is just comparing pointers, not strings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    Wow. So simple! Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Piping/Redirection/Background processing help needed
    By cr80expert5 in forum C Programming
    Replies: 1
    Last Post: 04-27-2009, 10:25 PM
  2. Using a lot of processing time!
    By nickname_changed in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2003, 03:44 AM
  3. Processing command line args...
    By Arker in forum C Programming
    Replies: 2
    Last Post: 03-21-2003, 01:55 PM
  4. file writing crashes
    By test in forum C Programming
    Replies: 25
    Last Post: 08-13-2002, 08:44 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM