argc, **argv in C++.

This is a discussion on argc, **argv in C++. within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm coding a program which I want to communicate with user while calling it. And is it neat to ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    argc, **argv in C++.

    Hello,
    I'm coding a program which I want to communicate with user while calling it. And is it neat to use int main(int argc, char *argv[]) in C++? Or maybe there are other, better technics in C++ to deal with such a problem ?
    Regards,
    apacz.

  2. #2
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    you want to communicate with a user...while calling what??
    do you mean you want to get input from the user? the argc, argv parameters are if you want to process command line arguments so that when someone would run your program, they would enter something like:
    programName /output
    in order to get the program to output whatever it was supposed to.
    Registered Linux User #380033. Be counted: http://counter.li.org

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    76
    while calling it (a program). I know how works argc, argv. I just asked if it's neat to use it when I'm coding in C++ - because I rather connect that mechanism with pure C. Probably "communicate" wasn't the best word to describe what I wanted - just injecting some information from user to a program and not using streams.
    Regards,
    apacz.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    I just asked if it's neat to use it when I'm coding in C++ - because I rather connect that mechanism with pure C.
    As far as I know, it is normal to use such command line argument handling in C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>int main(int argc, char *argv[])

    c++ standards did not change the way main() is declared. Its the same in c++ as it is in C. You might check boost libraries to see if it has a class that parses the arguments, similar to maybe getopt() in linux.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    int main(int argc, char* argv[]) is normal for C++, however, if you'd prefer to have the commandline arguments inside a std::vector<std::string> you could always do something like this at the beginning of main -
    Code:
    #include <vector>
    #include <string>
    
    int main(int argc, char* argv[])
    {
        std::vector<std::string> args;
        for(int i(0); i!=argc; ++i)
            args.push_back( std::string(argv[i]) );
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Or:
    Code:
    std::vector<std::string> args(argv, argv+argc);

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    The STL will act on language pointers as iterators? Cool.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,211
    Yup. As far as the STL is concerned, a pointer is just a particular type of iterator. Formally, iterators are a type of object that supports certain operations. And a raw pointer (IIRC) supports the same operations required of a random access iterator. This is no accident: iterators were designed to be a generalisation of the concept of a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 10:13 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 05:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  4. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  5. Replies: 18
    Last Post: 06-21-2003, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21