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.
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.
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
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.
As far as I know, it is normal to use such command line argument handling in C++.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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>>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.
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]) ); }
Or:Code:std::vector<std::string> args(argv, argv+argc);
The STL will act on language pointers as iterators? Cool.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
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.