By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,261 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,261 IT Pros & Developers. It's quick & easy.

integers as a string

P: n/a
If anyone could help me with my problem I'd greatly appreciate it, this
question probably has a quick easy answer but I've been wanting to punch the
wall for the last hour because of it.

I have a string IDNUM = "12345";

I need to do simple arithmetic with the integers in this string.

The way I've been trying and failing is by doing something similar to this:

int x , y;
string i1=IDNUM.substr(0,1); //sets i1 = 1 hopefully
x = atoi(i1); //Here, I fail to convert the integer in the string to
the type integer
y = x + 5;
I know the "x = atoi(i1)" is the root of all of my problems. But I do not
know how to fix it, and I have two books both of which do not touch on
atoi( ) at all. (buying a new book right now isn't the most convenient
option). At the time this section of code executes, the integers in the
string could be anything, so I can't just plug in the numbers.

So to sum up my question, I have a string of integers named IDNUM, and I
cannot figure out how to use the integers in this string outside of
outputting the string and then having the user re-enter the numbers they see
into an INT variable, which is stupid. Thanks in advance for any help.

-Mike V

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Nevermind, I just googled it and got the exact answer I needed. Probably
should have done that beforehand.

-Mike V
Jul 19 '05 #2

P: n/a

"Mike Vallely" <mk*************@hotmail.com> wrote in message
news:HU******************@newssvr31.news.prodigy.c om...
If anyone could help me with my problem I'd greatly appreciate it, this
question probably has a quick easy answer but I've been wanting to punch the wall for the last hour because of it.

I have a string IDNUM = "12345";

I need to do simple arithmetic with the integers in this string.

The way I've been trying and failing is by doing something similar to this:
int x , y;
string i1=IDNUM.substr(0,1); //sets i1 = 1 hopefully
x = atoi(i1); //Here, I fail to convert the integer in the string to
the type integer
y = x + 5;
I know the "x = atoi(i1)" is the root of all of my problems. But I do not
know how to fix it, and I have two books both of which do not touch on
atoi( ) at all. (buying a new book right now isn't the most convenient
option). At the time this section of code executes, the integers in the
string could be anything, so I can't just plug in the numbers.

So to sum up my question, I have a string of integers named IDNUM, and I
cannot figure out how to use the integers in this string outside of
outputting the string and then having the user re-enter the numbers they see into an INT variable, which is stupid. Thanks in advance for any help.

-Mike V


The root of the problem is that you don't know how to use string.

string str = "12345";
int x = atoi(str.c_str());
Jul 19 '05 #3

P: n/a
Hi

atoi() takes a const char* as an argument, not an std::string. So you
would need to do atoi(i1.c_str());

You could also use stringstreams instead of atoi() to convert an
std::string into an int, something like this
template<typename RT, typename T, typename Trait, typename Alloc>
RT ss_atoi( std::basic_string<T, Trait, Alloc>& the_string )
{
std::basic_istringstream< T, Trait, Alloc> temp_ss(the_string);
RT num;
temp_ss >> num;
return num;
}

Which you would call as int x = ss_atoi<int>(i1);
Or you could use boost::lexical_cast.

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.