Thread: RE: [Dev-C++] string-version of '+'
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: James G. <Jam...@Cl...> - 2000-12-07 16:09:47
|
this might not be the best way but I've always had to split the code into
two statements.
Change this:
myString = "John has " + myApples + " apples";
To this:
myString = "John has " + myApples;
myString += " apples";
This is because it works from right to left.
In your code you are trying to assign the string " apples" to the char[20]
and the result from that to the string "John has " and then that lot to the
std::string.
I think that's correct...
Regards,
James.
-----Original Message-----
From: Gregor Peter [mailto:Gre...@dl...]
Sent: Thursday, December 07, 2000 2:58 PM
To: exc...@sp...; dev...@li...
Subject: [Dev-C++] string-version of '+'
You're right!
I tried it before in a direkt manner which doesn't work :-(
string myString;
char myApples[20];
int myInt;
itoa( myInt, myApples, 10);
myString = "John has " + myApples + " apples";
I get following error message:
invalid operands `const char[28]' and `char[20]' to binary `operator +'
How do I enshure to use the string-version of '+' instead of the integer?
Gregor
> string my1String("John has");
> string my2String("apples!");
> char myApples[20] ;
> int myInt = 6;
>
> itoa(myInt, myApples, 10);
>
> my1String = my1String + myApples + my2String ;
>
> That is you can put all of the strings into one, my1String.
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
http://lists.sourceforge.net/mailman/listinfo/dev-cpp-users
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
This electronic message and any attachment is intended to be
read by the named addressee(s) only.
Any other recipient should be aware that its contents may be
legally privileged and/or confidential and that its use,
disclosure, copying or distribution may be unlawful.
Unless you are a named addressee, please delete this message
Whilst C. & J. Clark International Limited has taken steps
to prevent the transmission of computer viruses with electronic mail,
responsibility for screening incoming messages and the risk of such
transmission and its consequences lies with the recipient.
C. & J. Clark International Limited
Registered in England and Wales
Company No. 141015
Registered Office: 40 High Street, Street, Somerset BA16 0YA
Telephone: +44 (0) 1458 443131
Fax: +44 (0) 1458 447547
|
|
From: Chad S. <ho...@ho...> - 2000-12-07 16:32:22
|
Or you can do it with parentheses.. string myString; ((myString += "John has ") += myApples ) += " apples"; But this sort of thing should really be done with a strstream. Perhaps if Gregor could show more specifically what his code looks like I could explain how he's using the strstream wrong. Chad Simmons >From: James Gordon <Jam...@Cl...> >Reply-To: dev...@li... >To: "'dev...@li...'" ><dev...@li...> >Subject: RE: [Dev-C++] string-version of '+' >Date: Thu, 7 Dec 2000 15:34:59 -0000 > >this might not be the best way but I've always had to split the code into >two statements. > >Change this: >myString = "John has " + myApples + " apples"; > >To this: >myString = "John has " + myApples; >myString += " apples"; > [Snip] > >James. > > >-----Original Message----- >From: Gregor Peter [mailto:Gre...@dl...] >Sent: Thursday, December 07, 2000 2:58 PM >To: exc...@sp...; dev...@li... >Subject: [Dev-C++] string-version of '+' > > >You're right! > >I tried it before in a direkt manner which doesn't work :-( > >string myString; >char myApples[20]; >int myInt; > >itoa( myInt, myApples, 10); >myString = "John has " + myApples + " apples"; > >I get following error message: > invalid operands `const char[28]' and `char[20]' to binary `operator >+' _____________________________________________________________________________________ Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com |
|
From: Chad S. <ho...@ho...> - 2000-12-07 16:44:33
|
Interesting.. I decided to actually test the code I was promoting, and I
found a problem with the compiler.. It seems to bomb out on this bit of
code..
#include <string>
#include <iostream>
#include <strstream>
using namespace std;
int main(int argc, char *argv[])
{
string MyString;
strstream MyStream;
((MyString += "John has ") += "4") += " apples!";
cout << MyString << endl;
MyStream << "John has " << 10 << " apples!";
MyString = MyStream.str();
cout << MyString << endl;
return 0;
}
But I can compile the exact same code using the GNU G++ compiler without any
errors or warnings or crashes.
So this most likely explains Gregor's problems.. But the fault is with the
compiler, as the Standard C++ library dictates this functionality.
Chad Simmons
_____________________________________________________________________________________
Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com
|
|
From: Chad S. <ho...@ho...> - 2000-12-07 16:57:52
|
Sorry, I need to clarify.. It's the
MyString = MyStream.str();
line that the compiler bombs on.
This other line with the parentheses is ok.
((MyString += "John has ") += "4") += " apples!";
Chad Simmons
>From: "Chad Simmons" <ho...@ho...>
>Reply-To: dev...@li...
>To: dev...@li...
>Subject: RE: [Dev-C++] string-version of '+'
>Date: Thu, 07 Dec 2000 08:44:28 -0800
>
>Interesting.. I decided to actually test the code I was promoting, and I
>found a problem with the compiler.. It seems to bomb out on this bit of
>code..
>
>#include <string>
>#include <iostream>
>#include <strstream>
>using namespace std;
>
>int main(int argc, char *argv[])
>{
> string MyString;
> strstream MyStream;
>
> ((MyString += "John has ") += "4") += " apples!";
>
> cout << MyString << endl;
>
> MyStream << "John has " << 10 << " apples!";
>
> MyString = MyStream.str();
>
> cout << MyString << endl;
>
> return 0;
>}
_____________________________________________________________________________________
Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com
|
|
From: James G. <Jam...@Cl...> - 2000-12-08 09:07:36
|
Your using strstream the wrong way, in the code below I'm using ostrstream
(output string stream) to be able to use the << operators.
#include <string>
#include <iostream>
#include <strstream>
using namespace std;
int main(int argc, char *argv[])
{
string MyString;
char MyChar[200];
((MyString += "John has ") += "4") += " apples!";
cout << MyString << endl;
// Wrap the char in an ostratream to enable the use of '<<' operator the
same as cout etc.
ostrstream(MyChar, sizeof(MyChar)) << "John has " << 10 << " apples!" <<
ends;
MyString = MyChar;
cout << MyString << endl;
return 0;
}
Regards,
James.
-----Original Message-----
From: Chad Simmons [mailto:ho...@ho...]
Sent: Thursday, December 07, 2000 4:58 PM
To: dev...@li...
Subject: RE: [Dev-C++] string-version of '+'
Sorry, I need to clarify.. It's the
MyString = MyStream.str();
line that the compiler bombs on.
This other line with the parentheses is ok.
((MyString += "John has ") += "4") += " apples!";
Chad Simmons
>From: "Chad Simmons" <ho...@ho...>
>Reply-To: dev...@li...
>To: dev...@li...
>Subject: RE: [Dev-C++] string-version of '+'
>Date: Thu, 07 Dec 2000 08:44:28 -0800
>
>Interesting.. I decided to actually test the code I was promoting, and I
>found a problem with the compiler.. It seems to bomb out on this bit of
>code..
>
>#include <string>
>#include <iostream>
>#include <strstream>
>using namespace std;
>
>int main(int argc, char *argv[])
>{
> string MyString;
> strstream MyStream;
>
> ((MyString += "John has ") += "4") += " apples!";
>
> cout << MyString << endl;
>
> MyStream << "John has " << 10 << " apples!";
>
> MyString = MyStream.str();
>
> cout << MyString << endl;
>
> return 0;
>}
____________________________________________________________________________
_________
Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
http://lists.sourceforge.net/mailman/listinfo/dev-cpp-users
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
This electronic message and any attachment is intended to be
read by the named addressee(s) only.
Any other recipient should be aware that its contents may be
legally privileged and/or confidential and that its use,
disclosure, copying or distribution may be unlawful.
Unless you are a named addressee, please delete this message
Whilst C. & J. Clark International Limited has taken steps
to prevent the transmission of computer viruses with electronic mail,
responsibility for screening incoming messages and the risk of such
transmission and its consequences lies with the recipient.
C. & J. Clark International Limited
Registered in England and Wales
Company No. 141015
Registered Office: 40 High Street, Street, Somerset BA16 0YA
Telephone: +44 (0) 1458 443131
Fax: +44 (0) 1458 447547
|