string type casting to int

E

Eli Luong

I had a string element, using the string class, and I'm trying to
convert this to an integer. I took a substring of the string because
that is where the integer is located, and I tried to use atoi() on it,
but it seems atoi() accepts char* type variable, whereas what I have is
a string. I've tried searching for a method to convert a string to an
integer, but haven't found a clear answer and was hoping someone here
can help.

Thanks,
- Eli
 
V

Victor Bazarov

Eli said:
I had a string element, using the string class, and I'm trying to
convert this to an integer. I took a substring of the string because
that is where the integer is located, and I tried to use atoi() on it,
but it seems atoi() accepts char* type variable, whereas what I have
is a string. I've tried searching for a method to convert a string to
an integer, but haven't found a clear answer and was hoping someone
here can help.

Use 'strtol' instead of 'atoi' and read up on 'c_str' member of the
std::string class.

V
 
A

atomik.fungus

Eli Luong ha escrito:
I had a string element, using the string class, and I'm trying to
convert this to an integer. I took a substring of the string because
that is where the integer is located, and I tried to use atoi() on it,
but it seems atoi() accepts char* type variable, whereas what I have is
a string. I've tried searching for a method to convert a string to an
integer, but haven't found a clear answer and was hoping someone here
can help.

Thanks,
- Eli

I had to do something similar once
You can try to make a new function which creates a an array of char's
big enough to fit in anything you want and read the string element by
element copying it in your array. Once it is done you can use atoi.
First you must find the length of the string.

Note that im a newbie and there is probably an easier and more
effective way
 
M

Marcus Kwok

Victor Bazarov said:
Use 'strtol' instead of 'atoi' and read up on 'c_str' member of the
std::string class.

There is also the method using stringstreams given in the FAQ.
 
L

Lars Tetzlaff

Eli said:
I had a string element, using the string class, and I'm trying to
convert this to an integer. I took a substring of the string because
that is where the integer is located, and I tried to use atoi() on it,
but it seems atoi() accepts char* type variable, whereas what I have is
a string. I've tried searching for a method to convert a string to an
integer, but haven't found a clear answer and was hoping someone here
can help.

Thanks,
- Eli

You may try boost::lexical_cast like:

int aVal = boost::lexical_cast<int>( "1234" );

http://www.boost.org/libs/conversion/lexical_cast.htm#examples
 

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

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top