string to int conversion

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2007
Posts: 266
Reputation: quintoncoert is an unknown quantity at this point 
Solved Threads: 3
quintoncoert quintoncoert is offline Offline
Posting Whiz in Training

string to int conversion

 
0
  #1
May 30th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: string to int conversion

 
0
  #2
May 30th, 2007
the parameters to main() can be in one of two forms
  1. int main(int argc, char **argv)
  2. {
  3. }
  4.  
  5. or
  6. int main(int argc, char *argv[])
  7. {
  8. }

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).
  1. int main(int argc, char **argv)
  2. {
  3. int n = atoi(argv[2]);
  4. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: string to int conversion

 
0
  #3
May 30th, 2007
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

  1. string command_line_arg ="1234"
  2.  
  3. int x;
  4.  
  5. x = atoi(command_line_arg);


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

works well for me...just try....
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: string to int conversion

 
0
  #4
May 30th, 2007
  1. #include <boost/lexical_cast.hpp>
  2. int main( int argc, char* argv[] )
  3. {
  4. if( argc> 2 )
  5. {
  6. try
  7. {
  8. int value = boost::lexical_cast<int>( argv[2] ) ;
  9. // use value
  10. }
  11. catch( const boost::bad_lexical_cast& )
  12. {
  13. // cast error
  14. }
  15. }
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/wg...006/n1973.html
so we might as well start using it.
Last edited by vijayan121; May 30th, 2007 at 8:52 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC