I am writting a program which receives args on the command line. Before they can be used however they must be converted to ints. I am using vc++ express edition and I have tried the _strtoi64 function but I cannot get the parameters to this function right. It is specifically the second parameter of this function which gives problems. Is asks for a ** char to show what should stop the function scanning for numbers to convert to int. The problem is that, being new to c++, I have no idea what to pass as a second parameter. I have just secured some sort of grip on pointer but this double pointer like syntax is a bit much just now. In addition the first parameter is just a number in char form. Why cant the function just read the first parameter to its end and then return the converted int when it encounters the end? Can someone please show me to use this function correctly or perhaps tell me of another one which is easier to use?

the parameters to main() can be in one of two forms

int main(int argc, char **argv)
{
}

or
int main(int argc, char *argv[])
{
}

Both the above are identical and access individual arguments the same way. Convert the third argument like this (recall argv[0] is the name of the program).

int main(int argc, char **argv)
{
   int n = atoi(argv[2]);
}

There are several functions that convert a string to int or long and all are normally acceptable, atoi() is only one of them. atoi() does not provide for any error checking, so if you are concerned about possible errors in the argument string then you should use one of the other functions.

i don't know if this will work...just try this one...
you have a string from the command line, you store that string into a variable then convert it using atoi() function

string command_line_arg ="1234"

int x;

x = atoi(command_line_arg);

don't forget to include the header <stdlib.h>

works well for me...just try....

#include <boost/lexical_cast.hpp>
int main( int argc, char* argv[] )
{
    if( argc> 2 )
    {
      try 
      {
        int value = boost::lexical_cast<int>( argv[2] ) ;
        // use value
      }
      catch( const boost::bad_lexical_cast& )
      {
        // cast error
      } 
}

this is part of the proposal for c++0x (in TR2) and is almost certainly going to be accepted. see: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1973.html
so we might as well start using it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.