PDA

Click to See Complete Forum and Search --> : Newbie string addition


sql_lall
Sep 29th, 2002, 06:04 AM
I know this will be easy, just that i am new to the string side of C++ (i can understand the maths parts as it's similar to VB, which i am better at)
I just am wondering the most efficient way to do the equivalent of this:

Dim a$, b$
a$ = "he"
b$ = "llo"
a$ = a$ + b$
Print a$

kedaman
Sep 29th, 2002, 08:04 AM
you concentarte c strings with strcat, the string class should have a overloaded operator +

parksie
Sep 29th, 2002, 10:53 AM
Originally posted by sql_lall
I know this will be easy, just that i am new to the string side of C++ (i can understand the maths parts as it's similar to VB, which i am better at)
I just am wondering the most efficient way to do the equivalent of this:

Dim a$, b$
a$ = "he"
b$ = "llo"
a$ = a$ + b$
Print a$ string a("he");
string b("llo");

a += b;

cout << a << endl;

sql_lall
Sep 30th, 2002, 05:14 AM
I think i'll use the strcat method. However, i was unaware that you could declare strings like that and use them. What versions of C++ can you do that in??

parksie
Sep 30th, 2002, 07:16 AM
Um, you can do it in C++, C++, or C++.

strcat is for C-style strings declared as pointers/arrays. C++ is slightly more intelligent about it.

CornedBee
Sep 30th, 2002, 12:40 PM
For the string class you need these two lines at the top of your app:

#include <string>
using namespace std;


The include directive imports the file "string" into your app (which is where the string class is declared). Don't worry about the other line for now, wait until you learn about namespaces. It's not very high priority. Just remember you need it everytime you include any headers from the C++ standard library.

sql_lall
Oct 5th, 2002, 06:04 AM
Ok, i tried the using namespace std; thing, but it didn't work.
Just a question, should it matter whether the i #included <string> or <string.h>, of for that matter is there any difference between <[headername]> and <[headername].h>??

Zaei
Oct 5th, 2002, 07:26 AM
If there is a .h at the end, and there is another ehader without it, the one without is newer, and should be used.

Z.

sql_lall
Oct 6th, 2002, 04:54 AM
Yeah, i had to use no .h
It solved the problem, but i couldn't cout<< the strings.
Any suggestions. ( i just tried the code posted before, with the necessary headings, but the cout line came up with an error)

parksie
Oct 6th, 2002, 05:15 AM
You need <iostream>, not <iostream.h> or it won't work properly.

sql_lall
Oct 7th, 2002, 04:57 AM
Thanks for your answer. Very helpful.
BTW: your picture, with which smilies does the animation 'start' :p ??

parksie
Oct 7th, 2002, 06:14 AM
It starts with the cool guy getting an idea ;)

It was made by Bloodeye (I think) who I haven't seen around here for ages now...