Topic: << HELP >> Int to Char???
Author: ddkilzer@iastate.edu (David D Kilzer)
Date: 1995/05/30 Raw View
mykland@samson.tmlh.no (Bjorn Tore Mykland ) writes:
>Could someone help me??
>I want to convert a number (int), so that I can use it in a
>StringArray.
>ex:
> int Number = 12;
> char CharText[LineLength];
> CharText = "this is a test ";
> char NumText= <number to NumText>; ???
> strcat(CharText, numtext);
> cout << CharText << endl;
>Should give -- > this is a test 12 (as text)
>I know that i can use 'int' directly into 'cout', but the point is that I
>must use it to generate a filenames like: filename1, filename2,
>filename3.......
Open a stream to your character array (CharText). Then just ``print''
the text and the number to the string as you would with any other
stream:
#include <strstream.h>
ostrstream oCharText(CharText, LineLength);
oCharText << "test is a test " << Number;
oCharText.close(); // Not needed, but a Good Idea(tm)
cout << CharText << endl;
Dave ``Do it with streams'' Kilzer
--
David D. Kilzer \ Emergency Holographic Doctor:
ddkilzer@iastate.edu / ``Don't worry, I'm not going to kiss you.
Computer Engineer 4 \ I'm only adjusting the restraint.''
Iowa State University, Ames / _Star_Trek_Voyager:__Phage_
Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1995/05/31 Raw View
In article <3qfb65$ft6@natasha.rmii.com> mike@if.com (MIke Homyack -
System Owner) writes:
|> On Mon, 29 May 1995 23:12:17, Bjorn Tore Mykland (mykland@samson.tmlh.no) wrote:
|> [snip]
|> - ex:
|> - int Number = 12;
|> - char CharText[LineLength];
|> - CharText = "this is a test ";
|> - char NumText= <number to NumText>; ???
|> - strcat(CharText, numtext);
|> - cout << CharText << endl;
|> - Should give -- > this is a test 12 (as text)
|> - I know that i can use 'int' directly into 'cout', but the point is that I
|> - must use it to generate a filenames like: filename1, filename2,
|> - filename3.......
|> - Mykland :)
|> Presuming NumText is a char array big enough to hold the converted number,
|> why not simply use sprintf?
|> ie.
|> sprintf(NumText, "%d", Number);
|> This may not be the coolest, OOP way of doing this, but it works for me. I
|> suppose that you could obfuscate things by wrapping the sprintf in another
|> function or even create a method for a numeric class that returns that number
|> as a string... but it's probably overkill in this case.
A better solution might be to simply use ostrstream. If performance
is not a particular problem, you can even leave the entire memory
management to ostrstream, with the result that you don't have to know
how big the destination string should be:
ostrstream numText ;
numText << "filename" << number << ends ;
char const* p = numText->str() ;
At this point, *you* own the buffer, and are responsible for either
freeing it (delete [] p) or returning ownership to ostrstream
(numText.rdbuf()->freeze( 0 )). I generally prefer the latter, but it
*does* invalidate your pointer.
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1995/05/31 Raw View
In article <ddkilzer.801861797@tbp.eng.iastate.edu>
ddkilzer@iastate.edu (David D Kilzer) writes:
|> ostrstream oCharText(CharText, LineLength);
|> oCharText << "test is a test " << Number;
|> oCharText.close(); // Not needed, but a Good Idea(tm)
Does your ostrstream even have a `close' function? Mine doesn't (and
ostringstream in the draft standard doesn't either). On the other
hand, if you do want to get at a C-like string, you should generally
output an `ends' somewhere.
|> cout << CharText << endl;
I think this should be:
cout << oCharText.rdbuf() << endl ;
But as the original poster said that he also wanted to use the string
in other contexts (as a filename, etc.), ostrstream.str() might of
interest. (Outputting the buffer, as above, does not require the
`ends', but accessing the stream as a string with `ostrstream.str()'
does.)
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
Author: mykland@samson.tmlh.no (Bjorn Tore Mykland )
Date: 1995/05/29 Raw View
Hello!
Could someone help me??
I want to convert a number (int), so that I can use it in a
StringArray.
ex:
int Number = 12;
char CharText[LineLength];
CharText = "this is a test";
char NumText= <number to NumText>; ???
strcat(CharText, numtext);
cout << CharText << endl;
Mykland
Author: mykland@samson.tmlh.no (Bjorn Tore Mykland )
Date: 1995/05/29 Raw View
Hello!
Could someone help me??
I want to convert a number (int), so that I can use it in a
StringArray.
ex:
int Number = 12;
char CharText[LineLength];
CharText = "this is a test ";
char NumText= <number to NumText>; ???
strcat(CharText, numtext);
cout << CharText << endl;
Should give -- > this is a test 12 (as text)
I know that i can use 'int' directly into 'cout', but the point is that I
must use it to generate a filenames like: filename1, filename2,
filename3.......
Mykland :)
Author: pstemari@erinet.com (Paul J. Ste. Marie)
Date: 1995/05/30 Raw View
Followup-To: comp.lang.c++
In article <mykland.173.00173514@samson.tmlh.no>,
mykland@samson.tmlh.no (Bjorn Tore Mykland ) wrote:
:Hello!
:
:Could someone help me??
:I want to convert a number (int), so that I can use it in a
:StringArray.
:
:ex:
:
: int Number = 12;
: char CharText[LineLength];
: CharText = "this is a test ";
:
: char NumText= <number to NumText>; ???
: strcat(CharText, numtext);
: cout << CharText << endl;
:
:
:Should give -- > this is a test 12 (as text)
:
:I know that i can use 'int' directly into 'cout', but the point
is that I
:must use it to generate a filenames like: filename1, filename2,
:filename3.......
(Wrong newsgroup, followups redirected)
Use an ostrstream, ala:
#include <strstream.h>
...
for (int n = 1; n <= 3; ++n) {
ostrstream filename;
filename << "filename" << n << ends;
// use filename.str() as required-it's a char*
delete[] filename.str();
}
filename << "file
--Paul J. Ste. Marie, pstemari@well.sf.ca.us, pstemari@erinet.com
The Financial Crimes Enforcement Network claims that they capture every
public posting that has their name ("FinCEN") in it. I wish them good hunting.
Author: mike@if.com (MIke Homyack - System Owner)
Date: 1995/05/30 Raw View
On Mon, 29 May 1995 23:12:17, Bjorn Tore Mykland (mykland@samson.tmlh.no) wrote:
[snip]
- ex:
- int Number = 12;
- char CharText[LineLength];
- CharText = "this is a test ";
- char NumText= <number to NumText>; ???
- strcat(CharText, numtext);
- cout << CharText << endl;
- Should give -- > this is a test 12 (as text)
- I know that i can use 'int' directly into 'cout', but the point is that I
- must use it to generate a filenames like: filename1, filename2,
- filename3.......
- Mykland :)
Presuming NumText is a char array big enough to hold the converted number,
why not simply use sprintf?
ie.
sprintf(NumText, "%d", Number);
This may not be the coolest, OOP way of doing this, but it works for me. I
suppose that you could obfuscate things by wrapping the sprintf in another
function or even create a method for a numeric class that returns that number
as a string... but it's probably overkill in this case.
--
== === = == === =============================================================
== = = === = === == Mr.H (mike@mrhappy.if.com) ==
== = = = == == All opinions expressed herein are mine alone... right? ==
== === = === = === == "Goodbye boys!" ... "Have fun storming the castle!" ==
== === = === = === =============================================================
All mail accepted... Encrypted mail preferred... E-Mail me for my public key
PGP fingerprint: CC BD FD 38 BD C2 FB 56 02 AA 3C 7A 68 BB AF 54