On Wed, 28 Jan 2004 09:32:08 +0100, "Chris Theis"
<Ch*************@nospam.cern.ch> wrote in comp.lang.c++:
"Patrik Stellmann" <st*******@tu-harburg.de> wrote in message
news:bv************@uni-berlin.de... I have a string :
string A = "2";
I need to get it's integer value :
// ??
int val = A ; // ??
assuming you include stdlib.h and the string-class provides the
cast-operator to const char*:
Sorry, but both of these assumptions are incorrect. The standard header to
include is <cstdlib> nowadays. Furthermore the string class does not provide
an implicit const char* conversion but rather the c_str() member function.
int val = atoi(string);
int val = atoi( A.c_str() );
should do the trick.
No, not really. The ato... functions from the C library should never
be used unless you have already checked the character array and know
that it will not result in a numeric value outside the range of the
given type, in which case you might as well do the conversion yourself
while you are walking the string.
All of the ato... functions generate undefined behavior if the text
string represents a numerical value outside the range of the type.
That is exactly why the strto... functions were added during C
standardization. They have defined behavior for all inputs, other
than being passed a null pointer.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html