| | |
string to int conversion
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2007
Posts: 266
Reputation:
Solved Threads: 3
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
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).
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.
C++ Syntax (Toggle Plain Text)
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).
C++ Syntax (Toggle Plain Text)
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.
Last edited by Ancient Dragon; May 30th, 2007 at 7:44 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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
don't forget to include the header <stdlib.h>
works well for me...just try....
you have a string from the command line, you store that string into a variable then convert it using atoi() function
C++ Syntax (Toggle Plain Text)
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....
Retreat!!!
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
C++ Syntax (Toggle Plain Text)
#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 } }
so we might as well start using it.
Last edited by vijayan121; May 30th, 2007 at 8:52 am.
![]() |
Similar Threads
- String to Int (C++)
- String to integer conversion (C)
Other Threads in the C++ Forum
- Previous Thread: Assertion failure in loading
- Next Thread: Problem while Type Casting
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






