C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Converting string to integer

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gil
Guest





PostPosted: Wed Jan 28, 2004 7:56 am    Post subject: Converting string to integer Reply with quote



I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ??
Back to top
Patrik Stellmann
Guest





PostPosted: Wed Jan 28, 2004 8:08 am    Post subject: Re: Converting string to integer Reply with quote



Quote:
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);


Back to top
Sharad Kala
Guest





PostPosted: Wed Jan 28, 2004 8:25 am    Post subject: Re: Converting string to integer Reply with quote




"Gil" <brightoceanlight (AT) hotmail (DOT) com> wrote

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



Back to top
Chris Theis
Guest





PostPosted: Wed Jan 28, 2004 8:32 am    Post subject: Re: Converting string to integer Reply with quote


"Patrik Stellmann" <stellmann (AT) tu-harburg (DOT) de> wrote

Quote:
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.

Quote:

int val = atoi(string);


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

should do the trick.

Regards
Chris



Back to top
Peter Koch Larsen
Guest





PostPosted: Wed Jan 28, 2004 9:14 am    Post subject: Re: Converting string to integer Reply with quote


"Gil" <brightoceanlight (AT) hotmail (DOT) com> skrev i en meddelelse
news:adc2ca29.0401272356.ed42b94 (AT) posting (DOT) google.com...
Quote:
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



Back to top
Armando
Guest





PostPosted: Wed Jan 28, 2004 10:25 am    Post subject: Re: Converting string to integer Reply with quote

[email]brightoceanlight (AT) hotmail (DOT) com[/email] (Gil) wrote in message news:<adc2ca29.0401272356.ed42b94 (AT) posting (DOT) google.com>...
Quote:
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

Back to top
Old Wolf
Guest





PostPosted: Wed Jan 28, 2004 8:19 pm    Post subject: Re: Converting string to integer Reply with quote

Quote:
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.

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

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

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

nowadays.

Back to top
Jack Klein
Guest





PostPosted: Thu Jan 29, 2004 3:06 am    Post subject: Re: Converting string to integer Reply with quote

On Wed, 28 Jan 2004 09:32:08 +0100, "Chris Theis"
<Christian.Theis (AT) nospam (DOT) cern.ch> wrote in comp.lang.c++:

Quote:

"Patrik Stellmann" <stellmann (AT) tu-harburg (DOT) de> wrote in message
news:bv7ql5$pch6m$1 (AT) uni-berlin (DOT) 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/~ajo/docs/FAQ-acllc.html

Back to top
Chris Theis
Guest





PostPosted: Thu Jan 29, 2004 7:58 am    Post subject: Re: Converting string to integer Reply with quote


"Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote

Quote:
On Wed, 28 Jan 2004 09:32:08 +0100, "Chris Theis"
[email]Christian.Theis (AT) nospam (DOT) cern.ch[/email]> wrote in comp.lang.c++:


[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



Back to top
Chris Theis
Guest





PostPosted: Thu Jan 29, 2004 8:01 am    Post subject: Re: Converting string to integer Reply with quote


"Old Wolf" <oldwolf (AT) inspire (DOT) net.nz> wrote

Quote:
I have a string :

string A = "2";

[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?

Quote:

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



Back to top
Default User
Guest





PostPosted: Thu Jan 29, 2004 5:07 pm    Post subject: Re: Converting string to integer Reply with quote

Chris Theis wrote:

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

Back to top
red floyd
Guest





PostPosted: Thu Jan 29, 2004 8:35 pm    Post subject: Re: Converting string to integer Reply with quote

Default User wrote:
Quote:
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.


Back to top
Ron Natalie
Guest





PostPosted: Thu Jan 29, 2004 9:03 pm    Post subject: Re: Converting string to integer Reply with quote


"red floyd" <no.spam (AT) here (DOT) dude> wrote

Quote:
Default User wrote:
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.


Back to top
Default User
Guest





PostPosted: Thu Jan 29, 2004 11:05 pm    Post subject: Re: Converting string to integer Reply with quote

Ron Natalie wrote:

Quote:
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.

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.