Converting a String to an Integer

S

sakitah

Hello Everyone,

Here's the problem: I have a string that I break down into 3 other
strings as such:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";


I want to manipulate these as numbers. I used the append function for
strings to fill the new strings from the original string. Sadly, I
cant append from the original string into an integer, so how can I do
it?


Thank You
Sakitah


Reply
 
V

Victor Bazarov

sakitah said:
Here's the problem: I have a string that I break down into 3 other
strings as such:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";


I want to manipulate these as numbers. I used the append function for
strings to fill the new strings from the original string. Sadly, I
cant append from the original string into an integer, so how can I do
it?

Search Google Groups for 'conversion int to string c++'. And please
do search Google for answers before posting. Otherwise you're wasting
your precious time waiting for an answer here.

V
 
M

mlimber

sakitah said:
Hello Everyone,

Here's the problem: I have a string that I break down into 3 other
strings as such:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";


I want to manipulate these as numbers. I used the append function for
strings to fill the new strings from the original string. Sadly, I
cant append from the original string into an integer, so how can I do
it?


Thank You
Sakitah


Reply

Please consult this FAQ:

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2

Cheers! --M
 
J

jalkadir

I hope this helps you.
As a word of advise, stay with the C++ style, but I am giving you the
two methodologies.

This is just a snip of a class I wrote to handle this kind of problem,
if you want the full class, just write to me directly using the same
subject title and I will send you a copy, OK?
--snip
const int jme::strtools::toInt( const std::string& s ) throw (
jme::strtoolsEx ) {
//Test the string
if ( s.empty() )
{
throw jme::strtoolsEx( str_empty, FILE, METHOD, LINE );
}
// This is what you want <<==
std::istringstream iss( s );
int num;
//If the data extracted from 'iss' is successfully assingned to
// num and iss retuns false
if ( iss >> num && iss.eof() )
return num;
else
throw jme::strtoolsEx( fe_eof_bit, FILE, METHOD, LINE ); <<==
this is my own exception, but it gives you an idea as to how to hadle
the problem.

/*
// The simple 'C' version
if(s.empty()) throw jme::strtoolsEx(STR_EMPTY,FILE,METHOD,LINE);
return atoi(s.c_str());
*/
}
 
M

Mark P

sakitah said:
Hello Everyone,

Here's the problem: I have a string that I break down into 3 other
strings as such:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";


I want to manipulate these as numbers. I used the append function for
strings to fill the new strings from the original string. Sadly, I
cant append from the original string into an integer, so how can I do
it?


Thank You
Sakitah


Reply

On top of what's been mentioned you may also want to look up strtol
 
D

Daniel Moree

sakitah said:
Hello Everyone,

Here's the problem: I have a string that I break down into 3 other
strings as such:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";


I want to manipulate these as numbers. I used the append function for
strings to fill the new strings from the original string. Sadly, I
cant append from the original string into an integer, so how can I do
it?


Thank You
Sakitah


Reply

From - Wed

Depending on what type of string you are using, you will need to use 2
types of functions.

if the string is
std:string
then use atoi()
if it is LPCTSTR then use
strtol

both are in the MSDN!

Daniel moree
 
K

Karl Heinz Buchegger

Daniel said:
if the string is
std:string
then use atoi()
if it is LPCTSTR then use
strtol

Why atoi() with std::string ?

Never recommend atoi(). It has a serious design flaw:
You cannot figure out if the conversion worked or produced
an error. atoi() is in the same category as gets(). Never, ever
recommend those functions. It would have been better if those
functions never were invented.
 
R

Ron Natalie

Karl said:
Why atoi() with std::string ?

Never recommend atoi(). It has a serious design flaw:
You cannot figure out if the conversion worked or produced
an error. atoi() is in the same category as gets(). Never, ever
recommend those functions. It would have been better if those
functions never were invented.
Further, in the case of an overflow, the behavior is undefined.
That's even worse than just not being able to differentiate between
0 and an error return.
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top