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 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.
"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
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.
> 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.
> 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
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
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
|
+- Client 1
|
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
|
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?
|
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")
|
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.
| |
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;
|
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)
{}
|
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;
}
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |