integers as a string

M

Mike Vallely

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
 
M

Mike Vallely

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

-Mike V
 
A

Ali R.

Mike Vallely said:
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());
 
T

Tim Threlfall

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.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top