Attaching two strings together

J

Joah Senegal

Hello all,

I';m getting fucking crazy out here. I want to kill people an all the
computer around. Well to the point... I;m trying to do something very very
simple (atleast it should be simple) I;m trying to put two strings together
basically.

I have sourcecode with a public string called currentstr. Currentstr is a
string that;s called everywhere in the source;

Currentstr=Currentstr+" -> "
printf ("%s", CurrentStr.c_str);

And I;ve tried some other things like

Try one:

string a;
string b;
string c;
a = currentstr;
b = "->"
c +=a
c+=b
printf ("%s", c.c_str);

Try two

string a;
string b;
string c;
a = currentstr;
b = "->"
c = a + b;
printf ("%s", c.c_str);

Well now... when I do printf only Currentstr is printed without "->"
Anyone know how to fix this.. I;ve searched the internet. but I can;t figure
it out

THANKS!!!!!
 
M

mlimber

Joah said:
Hello all,

I';m getting fucking crazy out here. I want to kill people an all the
computer around. Well to the point... I;m trying to do something very very
simple (atleast it should be simple) I;m trying to put two strings together
basically.

I have sourcecode with a public string called currentstr. Currentstr is a
string that;s called everywhere in the source;

Currentstr=Currentstr+" -> "
printf ("%s", CurrentStr.c_str);

First, your problem is that std::string::c_str() is a function, not a
data member. You need parentheses to invoke it.

Second, you should prefer iostreams to printf because it is type-safe.
See http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.1.

Cheers! --M
 
T

Thomas Tutone

Joah said:
I;m trying to do something very very
simple (atleast it should be simple) I;m trying to put two strings together
basically.

I have sourcecode with a public string called currentstr. Currentstr is a
string that;s called everywhere in the source;

Currentstr=Currentstr+" -> "
printf ("%s", CurrentStr.c_str);

And I;ve tried some other things like

Try one:

string a;
string b;
string c;
a = currentstr;
b = "->"
c +=a
c+=b
printf ("%s", c.c_str);

Try two

string a;
string b;
string c;
a = currentstr;
b = "->"
c = a + b;
printf ("%s", c.c_str);

Well now... when I do printf only Currentstr is printed without "->"
Anyone know how to fix this.. I;ve searched the internet. but I can;t figure
it out

Please copy and paste the following program, then compile it and
execute it. Please report back on what the output is.

#include <string>
#include <iostream>
#include <ostream>

int main()
{
using namespace std;
string currentstr("Joah");
cout << currentstr << endl;
currentstr = currentstr + " -> ";
cout << currentstr << endl;
string a;
string b;
string c;
a = currentstr;
b = "->";
c += a;
c += b;
cout << c << endl;
}

Best regards,

Tom
 
G

Gavin Deane

Joah said:
Hello all,

I';m getting fucking crazy out here. I want to kill people an all the
computer around. Well to the point... I;m trying to do something very very
simple (atleast it should be simple) I;m trying to put two strings together
basically.

I have sourcecode with a public string called currentstr. Currentstr is a
string that;s called everywhere in the source;

Currentstr=Currentstr+" -> "
printf ("%s", CurrentStr.c_str);

And I;ve tried some other things like

Try one:

string a;
string b;
string c;
a = currentstr;
b = "->"
c +=a
c+=b
printf ("%s", c.c_str);

Try two

string a;
string b;
string c;
a = currentstr;
b = "->"
c = a + b;
printf ("%s", c.c_str);

Well now... when I do printf only Currentstr is printed without "->"
Anyone know how to fix this.. I;ve searched the internet. but I can;t figure
it out

Please don't retype code into your message. It introduces errors (bits
of the above are not compileable C++) and means people have to guess
what your problem might be. Copy and paste directly from your editor
following the guidelines in
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

That said, does this code work for you? If not, explain waht's wrong
following the guidelines in that FAQ and you should get plenty of help.

#include <string>
#include <stdio.h>
using std::string;

int main()
{
string a = "a string";
string b = "->";
string c = a + b;
string d;
d += a;
d += b;

printf("%s\n", c.c_str());
printf("%s\n", d.c_str());
}

The output is
a string->
a string->

Gavin Deane
 
P

peter koch

Joah said:
Hello all,

I';m getting fucking crazy out here. I want to kill people an all the
computer around. Well to the point... I;m trying to do something very very
simple (atleast it should be simple) I;m trying to put two strings together
basically.

Wow! That sounds nice.
I have sourcecode with a public string called currentstr. Currentstr is a
string that;s called everywhere in the source;

Well... you don't really call a string so you? I'm thus not sure what
you mean, but Ill try anyway.
Currentstr=Currentstr+" -> "
printf ("%s", CurrentStr.c_str);

Well look at that one, dude. printf?? You really believe in using that
oldfashioned C-thingy? That is most impressive . I would stick to cout
if I were you.
And I;ve tried some other things like

Try one:

string a;
string b;
string c;
a = currentstr;
b = "->"
You are missing some semicolons. Also the teacher should have told you
to always initialise your variables as e.g.

string a = currentstr;
string b = "->";
and so on. More efficient - among other things.
c +=a
c+=b
printf ("%s", c.c_str);

Well - and now you do that C-style stuff again! You know it's better to
avoid that one, don't you? One reason is that it is not typesafe. And
actually that is the problem here: you pass a pointer to a function to
a procedure that (via the "%s" specifier) expects a char const*.
Try two

string a;
string b;
string c;
a = currentstr;
b = "->"
c = a + b;
printf ("%s", c.c_str);

Well now... when I do printf only Currentstr is printed without "->"
Anyone know how to fix this.. I;ve searched the internet. but I can;t figure
it out
Try std::cout instead. It's much nicer

/Peter
 
J

Joah Senegal

okay, but the c_str () method works in printf. the problem is that the
strings are not merged together somehow
 
L

LR

Joah said:
okay, but the c_str () method works in printf. the problem is that the
strings are not merged together somehow

Then please try to write a very small, but _complete_ program that has
the problem and post it, using cut and paste this time, here.

It's really hard to know what your problem is from what you've posted.
For example, I don't know what Currentstr is.

LR
 
J

Joah Senegal

thnx for al the replies,

I;ve solved the problem. The problem was this:

currentstr was build form a array of char.
the array was default filled with NULL values

and I filled the string like this
for (int i = 0; i < 50; i++){
Currentstr=currentar;
}

But that was wrong

I needed to do it like this:

int i = 0;
while (!currentar==NULL){
currenstr=currentstr+currentar;
i++;
}

So when I tried to print the currentstr after adding "->" the string was
like this:

Stringtext (big white
->

So when I tried to print the last part of the string was out of bounds of
the screen
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Joah said:
I;ve solved the problem. The problem was this:

currentstr was build form a array of char.
the array was default filled with NULL values

and I filled the string like this
for (int i = 0; i < 50; i++){
Currentstr=currentar;
}

But that was wrong

I needed to do it like this:

int i = 0;
while (!currentar==NULL){
currenstr=currentstr+currentar;
i++;
}

So when I tried to print the currentstr after adding "->" the string was
like this:

Stringtext (big white
->

So when I tried to print the last part of the string was out of bounds of
the screen


You are making several mistakes here. First one stylistic: NULL is intended
to be used with pointer, not chars. You can use it that way because is
defined as a value 0, but is confusing, is better to use '\0'.

Next, '\0' is not whitespace. If you use the result of c_str () you have a
C-style string, and the '\0' marks the end of the string, so printf and
family stop writing at his position.

And finally, you are mixing currenstr and Currentstr. Copy and paste from
real compilable code instead of retyping to avoid this confusions.
 
G

Gavin Deane

Joah said:
thnx for al the replies,

I;ve solved the problem. The problem was this:

currentstr was build form a array of char.
the array was default filled with NULL values

Well *nothing* in the hand typed nearly-code you posted had anything
like that in it, and none of the answers you got addressed your actual
problem. That perfectly illustrates the value of the advice in FAQ 5.8.
You've fixed the problem yourself, which is good. Your original post
didn't help other people to help you.

Gavin Deane
 
F

Frederick Gotham

Joah Senegal posted:
Currentstr=Currentstr+" -> "


Use the += operator:


Currentstr += " -> "

printf ("%s", CurrentStr.c_str);


You're missing function call parentheses, and you've also misspelled
"CurrentStr" several times.

printf("%s", currentstr.c_str() );

And I;ve tried some other things like

Try one:

string a;
string b;
string c;
a = currentstr;
b = "->"
c +=a
c+=b


You're missing a semi-colon after the three statements immediately above.

printf ("%s", c.c_str);


Again missing function call parentheses.

Try two

string a;
string b;
string c;
a = currentstr;
b = "->"


Missing semi-colon.

c = a + b;
printf ("%s", c.c_str);


Missing parentheses.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top