Addition of strings in C++

M

Matt

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
 
R

Ron Natalie

Matt said:
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.
 
M

Mike Wahler

Matt said:
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
 
J

Jerry Coffin

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.
 
M

Matt

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.
 
P

Peter van Merkerk

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.
 
J

Jerry Coffin

[ ... ]
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.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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.
 

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

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top