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 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.
"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
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.
> 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.
> 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
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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
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....
|
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,...
|
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 {...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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,...
|
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...
|
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...
| |