473,223 Members | 1,845 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,223 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 2870

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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.