Board index » cppbuilder » Adding two strings ?

Adding two strings ?


2004-06-03 05:28:42 PM
cppbuilder75
I am having some trouble working out how to add two strings to make one. For example:
char a,b,c;
a = "a";
b = "b";
c = a + b;
What do I need to do to allow this to work ?
Pete
 
 

Re:Adding two strings ?

Peter Moscatt wrote:
Quote
I am having some trouble working out how to add two strings to make one. For example:

char a,b,c;

a = "a";
b = "b";
c = a + b;

What do I need to do to allow this to work ?
Think that you need to start at the beginning.
First try to make one string.
Please report what the compiler tells about code like:
char a;
a = "a";
Then change the code in such a way that you can add
a string to a variable.
Hans.
 

Re:Adding two strings ?

// a, b and c are each a single char
char a,b,c;
// "a" is a 2-element character array, a
// char* style string consisting of the
// character 'a' followed by the string
// termination character, a character
// of binary value zero, a '\0'
// you cannot assign a character array
// to a character variable. They are different
// types
a = "a";
// same thing as above
b = "b";
// a character is a number so c = a + b; is
// a valid statement, but it does not do what
// you seem to want. 'c' is a char so after
// the line executes it will contain a number
// which is the sum of the numeric value of
// the character stored in variable 'a' plus
// that stored in variable 'b'
c = a + b;
#include <string.h>
char a[2] = "a"; // 'a' is a 2 element char array
// the above is identical to
// char a[2] = { 'a', '\0' };
// because a '\0' is used as the last char in a
// char* style string
char b[2] = "b"; // 'b' is a 2 element char array
char c[3]; // 'c' is a 3 element char array, contents undefined
strcpy(c, a); // c is now "a" (actually "a\0")
strcat(c, b); // c is now "ab" (actually "ab\0")
Read the help on 'strcpy' (string copy) and on 'strcat' (string
concatination). Note that there is no check that the receiving array
has enough space to accept what you are copying or concatinating.
Making sure that there is enough space is the task of the programmer
and not doing that is one of the more common mistakes of beginning
programmers.
C++ has a string class which makes things like this much easier.
For example, this does what I think you are trying to do:
#include <string.h.
string a = "a";
string b = "b";
string c;
c = a + b;
In the above the string class decided how much memory is required to
accept the concatinated strings a and b. If there was not enough then
it allocates a larger space for the result.
. Ed
Quote
Peter Moscatt wrote in message
news: XXXX@XXXXX.COM ...

I am having some trouble working out how to add
two strings to make one. For example:

char a,b,c;

a = "a";
b = "b";
c = a + b;

What do I need to do to allow this to work ?
 

{smallsort}

Re:Adding two strings ?

On 3 Jun 2004 02:28:42 -0700, Peter Moscatt wrote:
Quote
char a,b,c;

a = "a";
b = "b";
c = a + b;

What do I need to do to allow this to work ?
What did the compiler say?
Investigate the differences between char, char *, and std::string.
--
good luck,
liz
 

Re:Adding two strings ?

"Peter Moscatt" < XXXX@XXXXX.COM >wrote in message news:< XXXX@XXXXX.COM >...
Quote
I am having some trouble working out how to add two strings to make one. For example:

char a,b,c;

a = "a";
b = "b";
c = a + b;

What do I need to do to allow this to work ?

Pete
Assuming that you do wish to use strings[rather than char], then
use AnsiString data type
Samuel
 

Re:Adding two strings ?

G'Day Liz,
Thanks for the reply. I tryed using 'String' instead of 'char' in a hope that it might solve the problem but the compiler error comes back with a:
[C++ Error] UnitMain.cpp(84): E2085 Invalid pointer addition
Hope this sheds some light on the subject.
Pete
PS: The code looks like:
String tmp;
tmp = "inifile" + "\\evoxiniCreate.ini";
Liz Albin < XXXX@XXXXX.COM >wrote:
Quote
On 3 Jun 2004 02:28:42 -0700, Peter Moscatt wrote:

>char a,b,c;
>
>a = "a";
>b = "b";
>c = a + b;
>
>What do I need to do to allow this to work ?

What did the compiler say?

Investigate the differences between char, char *, and std::string.
--
good luck,
liz
 

Re:Adding two strings ?

Peter Moscatt wrote:
Quote
Thanks for the reply. I tryed using 'String' instead of 'char' in a hope that it might solve the problem but the compiler error comes back with a:
Please wrap your lines.
Quote
[C++ Error] UnitMain.cpp(84): E2085 Invalid pointer addition
You were about to forget posting the used code. Please
always begin with the code and then report the error message.
And indicate the exact line where the error occurs.
Quote
Hope this sheds some light on the subject.
String tmp;
tmp = "inifile" + "\\evoxiniCreate.ini";
This looks a different subject. Try:
tmp = "inifile" "\\evoxiniCreate.ini";
Hans.
 

Re:Adding two strings ?

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote
Peter Moscatt wrote:

>Thanks for the reply. I tryed using 'String' instead of 'char' in a hope that it might solve the problem but the compiler error comes back with a:


Please wrap your lines.

>[C++ Error] UnitMain.cpp(84): E2085 Invalid pointer addition

You were about to forget posting the used code. Please
always begin with the code and then report the error message.
And indicate the exact line where the error occurs.

>Hope this sheds some light on the subject.

>String tmp;
>tmp = "inifile" + "\\evoxiniCreate.ini";

This looks a different subject. Try:

tmp = "inifile" "\\evoxiniCreate.ini";

Hans.
Hi Hans,
Thanks mate..... that did the trick.
Thank you very much for the help on this one.
Sorry for the format of my previous posts.
Pete
 

Re:Adding two strings ?

"Peter Moscatt" < XXXX@XXXXX.COM >wrote in message
Quote
[C++ Error] UnitMain.cpp(84): E2085 Invalid pointer addition
You are trying to add two char* string pointers together. You can't do
that.
Quote
String tmp;
tmp = "inifile" + "\\evoxiniCreate.ini";
That should be this instead:
String tmp;
tmp = Stirng("inifile") + "\\evoxiniCreate.ini";
Or:
String tmp;
tmp = "inifile" + String("\\evoxiniCreate.ini");
Or:
String tmp;
tmp = "inifile\\evoxiniCreate.ini";
Gambit
 

Re:Adding two strings ?

Quote
That should be this instead:

String tmp;
tmp = Stirng("inifile") + "\\evoxiniCreate.ini";

Or:

String tmp;
tmp = "inifile" + String("\\evoxiniCreate.ini");

Or:

String tmp;
tmp = "inifile\\evoxiniCreate.ini";


Gambit


Or:
String tmp= "inifile";
tmp += "\\evoxiniCreate.ini";
hehehe.
silly me :)
Happy Programming! :)
 

Re:Adding two strings ?

"Peter Moscatt" < XXXX@XXXXX.COM >writes:
Quote
[C++ Error] UnitMain.cpp(84): E2085 Invalid pointer addition

PS: The code looks like:

String tmp;
tmp = "inifile" + "\\evoxiniCreate.ini";
A literal string like "inifile" is an array of const characters, the
array being the length of your string (plus nullbyte.)
Arrays are not String objects though, and they degrade to raw pointers
rather easily. Using arithmetic on them ensures that the array
becomes a pointer, and now you're merely adding two pointers.
Note: the right hand side of the = is evaluated in its entirety before
the assignment is done. Further, the fact that you're giving the
RESULT of this expression to a String object does not affect how the
expression is treated. The compiler does it in this order:
1) Evaluate "inifile" + "\\evoxiniCreate.ini";
2) Take result of step 1 and assign it to tmp
Notice step 1 doesn't consider tmp in any way, shape, nor form. Since
step1 is invalid, it never gets to step 2.
--
Chris (TeamB);