|
From: Chad S. <ho...@ho...> - 2000-12-06 18:06:43
|
>From: Gregor Peter <Gre...@dl...> >Reply-To: dev...@li... >To: "dev...@li..." ><dev...@li...> >Subject: [Dev-C++] int to string >Date: Wed, 06 Dec 2000 17:58:23 +0100 > >Read my question and you know that I'am a Neewbe :-) > >How to convert, cast, transform, whatever a int value to a string? > >include <string.h> > >string my1String = "John has"; >string my2String = "apples!"; >int myInt = 6; > >my1stString = my1stString + UNKNOWN_FUNC(myInt) + my2ndString; > >As you guessed I'd like to get: >"John has 6 apples!" How about this instead.. string my1String = "John"; string my2String = "apples"; int myInt = 6; strstream buffer; buffer << my1String << " has " << myInt << " "<< my2String << "!" << endl; cout << buffer.str(); // Output = John has 6 apples! Chad Simmons _____________________________________________________________________________________ Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com |
|
From: Chad S. <ho...@ho...> - 2000-12-06 18:51:00
|
>From: mpstarix <mps...@ya...>
>Reply-To: dev...@li...
>To: dev...@li...
>Subject: Re: [Dev-C++] int to string
>Date: Wed, 06 Dec 2000 19:22:51 +0100
>
>Try
>
>{
> char thestring[whatever]
>
> sprintf(whatever,"John has %d apples,muInt);
>}
>
>sprintf applies a format and then puts the result into a string
Right, but then you are dealing with a static string, and not a dynamic one.
Which raises issues of buffer overflows, etc. As long as you know the length
of the buffer cannot be exceeded you're ok. But it's often dificult to be
sure. The standard C++ string and strstream classes prevent such overflowing
by handling the memory allocation internally.
Chad Simmons
_____________________________________________________________________________________
Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com
|
|
From: James G. <Jam...@Cl...> - 2000-12-07 12:01:36
|
strstream is part of the C++ IO Streams. I believe it's in strstream. It's in dev-C++\include\G++, all it does it include strstream.h. Regards, James. -----Original Message----- From: Gregor Peter [mailto:Gre...@dl...] Sent: Thursday, December 07, 2000 11:20 AM To: dev...@li...; ho...@ho... Subject: Re: [Dev-C++] int to string Fine, but where to get strstream from? It isn't in iostream.h am I right? Are there any online documentation about stdlib.h, string.h & Co where I can browse for myself? Gregor Chad Simmons wrote: > >How to convert, cast, transform, whatever a int value to a string? > >include <string.h> > >string my1String = "John has"; string my2String = "apples!"; int myInt = 6; > >my1stString = my1stString + UNKNOWN_FUNC(myInt) + my2ndString; > >As you guessed I'd like to get: "John has 6 apples!" > > How about this instead.. > > string my1String = "John"; > string my2String = "apples"; > int myInt = 6; > strstream buffer; > > buffer << my1String << " has " << myInt << " "<< my2String << "!" << endl; > cout << buffer.str(); > // Output = John has 6 apples! _______________________________________________ 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: James G. <Jam...@Cl...> - 2000-12-07 13:47:26
|
Do you mean a string as in std::string or a string as in char[] or char*?
The function to do the latter is c_str().
Regards,
James.
-----Original Message-----
From: Gregor Peter [mailto:Gre...@dl...]
Sent: Thursday, December 07, 2000 1:27 PM
To: dev...@li...; ho...@ho...
Subject: Re: [Dev-C++] int to string
OK I got the library and tried it out, but ist doesn't work.
I need to feed needsStringFunc( string name )
so I tried
string name = buffer.str()
which causes my Programm to crash :-(
So the str() method doesn't return the content of buffer as a string?!?
Gregor
Chad Simmons wrote:
> How about this instead..
>
> string my1String = "John";
> string my2String = "apples";
> int myInt = 6;
> strstream buffer;
>
> buffer << my1String << " has " << myInt << " "<< my2String << "!" << endl;
>
> cout << buffer.str();
>
> // Output = John has 6 apples!
>
> Chad Simmons
>
____________________________________________________________________________
_________
> 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
_______________________________________________
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: Gregor P. <Gre...@dl...> - 2000-12-07 11:22:09
|
Fine, but where to get strstream from? It isn't in iostream.h am I right? Are there any online documentation about stdlib.h, string.h & Co where I can browse for myself? Gregor Chad Simmons wrote: > >How to convert, cast, transform, whatever a int value to a string? > >include <string.h> > >string my1String = "John has"; string my2String = "apples!"; int myInt = 6; > >my1stString = my1stString + UNKNOWN_FUNC(myInt) + my2ndString; > >As you guessed I'd like to get: "John has 6 apples!" > > How about this instead.. > > string my1String = "John"; > string my2String = "apples"; > int myInt = 6; > strstream buffer; > > buffer << my1String << " has " << myInt << " "<< my2String << "!" << endl; > cout << buffer.str(); > // Output = John has 6 apples! |
|
From: Gregor P. <Gre...@dl...> - 2000-12-07 13:29:02
|
OK I got the library and tried it out, but ist doesn't work.
I need to feed needsStringFunc( string name )
so I tried
string name = buffer.str()
which causes my Programm to crash :-(
So the str() method doesn't return the content of buffer as a string?!?
Gregor
Chad Simmons wrote:
> How about this instead..
>
> string my1String = "John";
> string my2String = "apples";
> int myInt = 6;
> strstream buffer;
>
> buffer << my1String << " has " << myInt << " "<< my2String << "!" << endl;
>
> cout << buffer.str();
>
> // Output = John has 6 apples!
>
> Chad Simmons
> _____________________________________________________________________________________
> 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
|