473,394 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2875

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example, I know the 18th through 21st byte is an integer...
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
11
by: H. | last post by:
I'm writing a driver for something I wrote, and can't remember how to do this very simple thing. The code I have at this point in time: int myArr; int temp; int ind = 0; cout << "enter some...
2
by: David T. Ashley | last post by:
I have a box where I'm forced to use PHP before 64-bit integers became standard ... If I run a MySQL query where one of the fields returned is 64-bit integer, how do I get this into PHP as a...
3
by: Jeff | last post by:
....still new to vb.net 2005 I understand the concept of arrays, and have used them in other languages, but was hoping that someone could get me started with something. I have a fairly long...
23
by: Summercool | last post by:
i think in Ruby, if you have an array (or list) of integers foo = you can use foo.join(",") to join them into a string "1,2,3" in Python... is the method to use ",".join() ? but then it...
2
by: SiJP | last post by:
Hi, My vb.net project sends an Input object to a webservice and retrieves a Results object. The webservice is maintained by a third party, and is pretty huge. I am using the following...
4
by: rmac | last post by:
the following code attempts to extract a symbol name from a string: extensionStart = int(filename.rfind('.')) filenameStart = int(filename.rfind('/')) #print 'Extension Start - ' +...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.