Ubuntu-Chicago Parsing argv in C++

Eric O'Neal fyedernoggersnodden at gmail.com
Mon Sep 11 02:39:36 BST 2006


Heya, I was hoping you guys could help me with some novice programming
woes.  I'm trying to parse command-line options (I want to write the parser
myself for the experience), and somehow am stuck up on... well... I dunno,
but it's not working.

The logic in my code is comparable to the following sample:

---------------------------

#include <iostream>

using namespace std;

int main( int argc, char *argv[] )
{
    for (int i = 1; i < argc; i++)
    {
        cout << "*Debug: Parsing(" << argv[i] << ")" << endl;
        if (argv[i] == "greetings")
        {
            cout << "Found: greetings" << endl;
        }
        else if (argv[i] == "g")
        {
            cout << "Found: g" << endl;
        }
        else
        {
            cout << "Invalid" << endl;
        }
    }
    return 0;
}

---------------------------

The above compiles fine with GCC v4.0.1 into a binary named "sandbox".  But
when I run it:

---------------------------

$ ./sandbox greetings
*Debug: Parsing(greetings)
Invalid
*Debug: Parsing(g)
Invalid

---------------------------

I though I had something working earlier, but whatever I changed broke it.
Help?

SigmaX

-- 
My home page:  http://www.SigmaX.org

"ttocs laeno cire oshkosh b'gosh fyedernoggersnodden nicht stein bon
probiscus"

"Education is what remains after one has forgotten everything he learned in
school"
              -- Albert Einstein
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.ubuntu.com/archives/ubuntu-us-chicago/attachments/20060910/cea0469e/attachment.htm 


More information about the Ubuntu-us-chicago mailing list