Many of my programs take in command-line arguments, one example is as follows:
a.out [file-name] [col#] [seed]
Then if I want to use the arguments, I have nice, easy-to-use functions such as:
atof(argv[..]) and atoi(argv[..])
I was wondering if such easy/simple functions exist for C++. I tried to simply do this:
cin >> col_num >> seed;
But that doesn’t work… It waits for an input (not command-line) and then outputs it…
Thanks
Solution 1:
You can use
lexical_castin place ofatoiUse
boost::lexical_castin try-catch block though. It throwsboost::bad_lexical_castwhen the cast is invalid.Solution 2:
If you are not using Boost and need a standard C++ solution you can use streams.