471,613 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,613 software developers and data experts.

integers as a string

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
3 2806

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

-Mike V
Jul 19 '05 #2

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

Similar topics

13 posts views Thread by Nicholas | last post: by
2 posts views Thread by David T. Ashley | last post: by
23 posts views Thread by Summercool | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.