473,382 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Attaching two strings together

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
10 4410
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

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

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

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
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alvin777 | last post by:
I'd like to have data file with many strings and load them at once using MapViewOfFile and unload with UnmapViewOfFile. So I wouldn't have to allocate memory for each string on heap. How can I...
6
by: Armando | last post by:
Hey gurus - Access 2000 - I have a form with a series of box controls forming a map of clickable areas, and I want each one to have a label. No problem, just put 'em on, right? But I also want...
2
by: Luis Arvayo | last post by:
Is there some way to attach an editor to a property of a class at runtime ? Ex.; public class MyClass { public int BitmapIndex
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
2
by: sri2097 | last post by:
Hi all, I have to select a particular file (using the 'Browse') button in Windows. After this I need to populate the 'Open Dialogue Box' with the path of the file I need (I have the entier path of...
52
by: Paddy | last post by:
I was browsing the Voidspace blog item on "Flattening Lists", and followed up on the use of sum to do the flattening. A solution was: I would not have thought of using sum in this way. When...
10
by: =?Utf-8?B?RGFyYSBQ?= | last post by:
Can some one suggest me as why StringBuilder class is better than Strings? -- Never say, Give up
1
by: jsavagedesign | last post by:
This is a project I have been working on lately and I keep running into problems. The idea is to be able to attaching an animation to a movie clip and call it later. Or multiple animation at once. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.