473,387 Members | 3,810 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,387 software developers and data experts.

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 6466

"Matt" <ma***@chilitech.net> wrote in message news:b5*************************@posting.google.co m...
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***@chilitech.net> wrote in message
news:b5*************************@posting.google.co m...
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*************************@posting.google.com> ,
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::stringstream uno;

uno << lexical_cast<int>(one) + lexical_cast<int>(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::stringstream uno;

uno << lexical_cast<int>(one) + lexical_cast<int>(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.merkerk(at)dse.nl
Jul 19 '05 #6
On Thu, 23 Oct 2003 13:57:57 +0200, "Peter van Merkerk"
<me*****@deadspam.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
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...
2
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...
7
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...
3
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 =...
3
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...
34
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...
8
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....
3
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,...
5
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 {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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...

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.