By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Attaching two strings together

P: n/a
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!!!!!
Sep 26 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Joah Senegal wrote:
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-lit....html#faq-15.1.

Cheers! --M

Sep 26 '06 #2

P: n/a

Joah Senegal wrote:
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

Sep 26 '06 #3

P: n/a

Joah Senegal wrote:
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-lit...t.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

Sep 26 '06 #4

P: n/a

Joah Senegal wrote:
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

Sep 26 '06 #5

P: n/a
okay, but the c_str () method works in printf. the problem is that the
strings are not merged together somehow
"mlimber" <ml*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Joah Senegal wrote:
>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-lit....html#faq-15.1.

Cheers! --M

Sep 26 '06 #6

P: n/a
LR
Joah Senegal wrote:
okay, but the c_str () method works in printf. the problem is that the
strings are not merged together somehow
>>>Currentstr=Currentstr+" -"
printf ("%s", CurrentStr.c_str);
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
Sep 26 '06 #7

P: n/a
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[i];
}

But that was wrong

I needed to do it like this:

int i = 0;
while (!currentar[i]==NULL){
currenstr=currentstr+currentar[i];
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

"Joah Senegal" <bl****@hva.nlwrote in message
news:45**********************@dreader26.news.xs4al l.nl...
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!!!!!

Sep 26 '06 #8

P: n/a
Joah Senegal wrote:
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[i];
}

But that was wrong

I needed to do it like this:

int i = 0;
while (!currentar[i]==NULL){
currenstr=currentstr+currentar[i];
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.

--
Salu2
Sep 26 '06 #9

P: n/a

Joah Senegal wrote:
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

Sep 26 '06 #10

P: n/a
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.

--

Frederick Gotham
Sep 26 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.