Converting string to integer

G

Gil

I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ??
 
P

Patrik Stellmann

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*:

int val = atoi(string);
 
S

Sharad Kala

Gil said:
I have a string :

string A = "2";

I need to get it's integer value :

Check http://www.groups.google.com

Anyways,
int main()
{
string str = "123";
istringstream iss(str);
int x;
iss >> x;
if(iss)
{
// conversion successful
}
else
{
// could not convert str to an int
}
}

Best wishes,
Sharad
 
C

Chris Theis

Patrik Stellmann said:
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
int val = atoi(string);

int val = atoi( A.c_str() );

should do the trick.

Regards
Chris
 
P

Peter Koch Larsen

Gil said:
I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ??

Do yourself two favours:

1) Get the FAQ for this newsgroup and read it all... it is well worth the
effort. The faq can be found at http://www.parashift.com/c++-faq-lite/.

2) Get the boost-library (http://www.boost.org) and look at all the
wonderful things it has to offer - among them the solution to your problem.

Kind regards
Peter
 
A

Armando

I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ??

hallo

i think you can do so which the function atoi.

int val = atoi(A);

Armando
 
O

Old Wolf

I have a string :
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.

He didn't mention any details about his string class. Maybe you are
confusing it with std::string ?
int val = atoi( A.c_str() );

int val = std::atoi( A.c_str() );

nowadays.
 
J

Jack Klein

Sorry, but both of these assumptions are incorrect. The standard header to


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.
 
C

Chris Theis

Jack Klein said:
[SNIP]

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.

I didn't know that ato... might result in undefined behavior. Thanks for
telling me, Jack. However, in C++ I would recommend to use stringstreams
anyway.

Cheers
Chris
 
C

Chris Theis

Old Wolf said:
[SNIP]

He didn't mention any details about his string class. Maybe you are
confusing it with std::string ?

The OP declared his string using a class named "string". Without any further
specification it is valid (at least IMHO) to presume that the standard
string class is meant. Anyway, naming your own string class like the
standard one is certainly not the cleverest idea, don't you think?
int val = std::atoi( A.c_str() );

nowadays.

Only if no using namespace statement has been issued, which cannot be
verified from the code snippet.

Chris
 
D

Default User

Chris said:
The OP declared his string using a class named "string". Without any further
specification it is valid (at least IMHO) to presume that the standard
string class is meant. Anyway, naming your own string class like the
standard one is certainly not the cleverest idea, don't you think?


I don't even think it's a legal identifier. I believe that in C++, like
C, all identifiers beginning with "str" and followed by a letter are
reserved.




Brian Rodenborn
 
R

red floyd

Default said:
Chris Theis wrote:
[redacted]
I don't even think it's a legal identifier. I believe that in C++, like
C, all identifiers beginning with "str" and followed by a letter are
reserved.

Say what??????????????? Can you quote chapter and verse from the Holy
Standard (or the Holy C standard) on that one?

That would break any code that uses "strange", "strong", "stretch",
etc.. as variables.
 
R

Ron Natalie

red floyd said:
Default said:
Chris Theis wrote:
[redacted]
I don't even think it's a legal identifier. I believe that in C++, like
C, all identifiers beginning with "str" and followed by a letter are
reserved.

Say what??????????????? Can you quote chapter and verse from the Holy
Standard (or the Holy C standard) on that one?
He's not quite got it right. The C standard reserves external symbols that
start with str followed by a lower case letter for future use.

You can use them internal to a function or class (or other internal linkage) or
in a namespace other than std or the global namespace.
 
D

Default User

Ron said:
He's not quite got it right. The C standard reserves external symbols that
start with str followed by a lower case letter for future use.

Yes, I forgot to mention the lowercase part.
You can use them internal to a function or class (or other internal linkage) or
in a namespace other than std or the global namespace.

As far as one could tell from example, it was in the global namespace.



Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top