473,699 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Addition of strings in C++

Hello,
How can I do mathematical addition of strings in C++?
As in:

string one;
string two;
string three;

three = one + two;

I don't want it to say "one two" but rather take the numbers input
into one and two and add them.

If this can't be done, what variable type can I use for string/int
etc? IE.. I want to be able to have strings or numbers in this
variable.. and want to be able to add it if it has numbers in it.

~ Matt
Jul 19 '05 #1
8 6484

"Matt" <ma***@chilitec h.net> wrote in message news:b5******** *************** **@posting.goog le.com...
Hello,
How can I do mathematical addition of strings in C++?
As in:

string one;
string two;
string three;

three = one + two;


You need to write two routines, one that translates words to numbers and another
that do the opposite.
Jul 19 '05 #2
"Matt" <ma***@chilitec h.net> wrote in message
news:b5******** *************** **@posting.goog le.com...
Hello,
How can I do mathematical addition of strings in C++?
By definition, strings are not mathematical entities.
As in:

string one;
string two;
string three;

three = one + two;

I don't want it to say "one two" but rather take the numbers input
into one and two and add them.


If the strings will contain the text representation of
numbers (e.g. "2"), then use stringstreams to extract
into numeric objects (e.g. 'int'), then do the arithmetic
and stream the result back into a string.

If you're wanting to store e.g. the English words "one"
"two", "three", etc. you'll need to write conversion routines.
(you could use a std::map to 'map' the numbers to the words.)

-Mike

Jul 19 '05 #3
In article <b5************ *************@p osting.google.c om>,
ma***@chilitech .net says...
Hello,
How can I do mathematical addition of strings in C++?
As in:

string one;
string two;
string three;

three = one + two;


I'd recommend against this, but if you insist, you have a couple of
possibilities. One would be to use Boost::lexical_ cast:

std::stringstre am uno;

uno << lexical_cast<in t>(one) + lexical_cast<in t>(two);
one = uno.str();

If you're going to do this a lot, you could write your own string class
to handle it, overloading operator+ to check whether a string represents
a number, and if so, adding the two as numbers, much as above.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #4
>
I'd recommend against this, but if you insist, you have a couple of
possibilities. One would be to use Boost::lexical_ cast:

std::stringstre am uno;

uno << lexical_cast<in t>(one) + lexical_cast<in t>(two);
one = uno.str();

If you're going to do this a lot, you could write your own string class
to handle it, overloading operator+ to check whether a string represents
a number, and if so, adding the two as numbers, much as above.


Perhaps I asked the wrong thing... maybe strings are not the way to
go.... is there some "variable" variable in C++ similar to
php/perl/VBA? That you can define and then put whatever you want in
it and do whatever you want to with it?

For instance in perl I can read a string/character/or number into the
variable
$blah
I can then also keep it as text or do mathematical computations on it.
Jul 19 '05 #5
> Perhaps I asked the wrong thing... maybe strings are not the way to
go.... is there some "variable" variable in C++ similar to
php/perl/VBA? That you can define and then put whatever you want in
it and do whatever you want to with it?

For instance in perl I can read a string/character/or number into the
variable
$blah
I can then also keep it as text or do mathematical computations on it.


Not in standard C++, but the boost library has (www.boost.org) an Any
class for this.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
Jul 19 '05 #6
On Thu, 23 Oct 2003 13:57:57 +0200, "Peter van Merkerk"
<me*****@deadsp am.com> wrote:
Perhaps I asked the wrong thing... maybe strings are not the way to
go.... is there some "variable" variable in C++ similar to
php/perl/VBA? That you can define and then put whatever you want in
it and do whatever you want to with it?

For instance in perl I can read a string/character/or number into the
variable
$blah
I can then also keep it as text or do mathematical computations on it.


Not in standard C++, but the boost library has (www.boost.org) an Any
class for this.


boost::any can't perform any conversions though, such as string->int.

Tom
Jul 19 '05 #7
In article <b5************ **************@ posting.google. com>,
ma***@chilitech .net says...

[ ... ]
Perhaps I asked the wrong thing... maybe strings are not the way to
go.... is there some "variable" variable in C++ similar to
php/perl/VBA? That you can define and then put whatever you want in
it and do whatever you want to with it?


C++ has everything necessary to do this in a class of your own, but you
_will_ have to do it yourself -- there's nothing built into the language
to do it.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #8
Matt escribió:
Perhaps I asked the wrong thing... maybe strings are not the way to
go.... is there some "variable" variable in C++ similar to
php/perl/VBA? That you can define and then put whatever you want in
it and do whatever you want to with it?


You can embed perl in your program and work with perl variables, for
example. Read the man page of perlembed.

Regards.
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1970
by: Jay Moore | last post by:
If any of you happened to read my earlier posts, I had a dilemma with creating an efficient method of limiting access to data for my users and subusers. My heirarchy looks like this: Admins | +- Client 1
2
1448
by: Will McGugan | last post by:
Hi, I'd like to suggest an addition to the slice syntax. How about allowing strings as arguments to slices on strings, and interpret them as the index of the substring found within the string being sliced. I'm sure I didnt explain that too well, so here is an example.. >>> a= "Find the string within the (parenthesis)." #equivalent of a >>> a
7
7697
by: jeffbernstein | last post by:
Greetings. I'm reading "How to think like a computer scientist: Learning with Python" and there's a question regarding string operations. The question is, "Can you think of a property that addition and multiplication have that string concatenation and repetition do not?" I thought it was the commutative property but "<string>"*3 is equivalent to 3*"<string>". Any ideas?
3
5220
by: Jay | last post by:
I have two strings that instead of adding them together to get the sum the are concatenating together. Does anyone know how I can get these two to add. while not rstemp4.eof vservdate = rstemp4("servdate") vdesc = rstemp4("desc") vservhours2 = rstemp4("servhours") vtravelhours = rstemp4("travelhours") vtech = rstemp4("tech")
3
6751
by: Rolf Hemmerling | last post by:
Hello ! Beginner's question: Howto access .RC/.RES Ressource files with portable C++ code ( BCC,MSVC,GNU-C++, OpenWatcom) ? I just wanna access "local language strings", so that I may replace the ..RC file to compile a version of my software for a different language.
34
16672
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1 - 3;
8
2592
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g. char * -> const char *. However, this does not appear to work when I add another level of indirection: void test1 (char **value) {}
3
2181
by: snow.carriers | last post by:
Let me first state that I'm using Borland Turbo C++, it's relatively old so the new string methods won't work. Anyways, first I'm trying to collect a line of a string (with numbers, letters, dashes) into each variable. For just numbers, it's relatively easy: ifstream fout("s1.in"); for (a=0; a<17; a++) { fout >> data; cout << data << endl; }
5
2277
by: Paul | last post by:
Why is the addition here adding the number as a string but the subtraction works fine? function boxchange(box) { iv= dbform.ut.value if (box.checked == true) { iv = iv - box.value } else { iv = iv + box.value
0
8687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8617
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8914
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.