Jeremy Cowles wrote:[color=blue]
>
> "Christoph Rabel" <odie@hal9000.vc-graz.ac.at> wrote in message
> news:3fd5a06a$0$18044$3b214f66@aconews.univie.ac.a t...[color=green]
> > wak wrote:[color=darkred]
> > > hi!
> > > how can I convert an int to char !?[/color]
> >
> > You can use a stringstream or sprintf.[/color]
>
> So C++ has no simple way to concat a string and other variables? For
> example, in other languages (using C++ syntax):
>
> //--
>
> String s("my string is");
> String n("long.");
> int i = 99;
>
> s += i + n;
> s += i.ToString( ) + n;
> s += ((string) i ) + n;
>
> //--
>
> You can't do that in C++?[/color]
You can do everything in C++.
It's just that some things don't come 'out of the box'.
For some things you need to do a little bit of work by yourself.
But once you have done that you have that functionality:
Taken from the FAQ and modified a little bit
inline std::string stringify(int x)
{
std::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}
can be used as in:
std::string s( "my string is" );
std::string n( "long." );
int i = 99;
s += stringify( i ) + n;
You might also go to
http://www.boost.org and look up lexical_cast.
Basically it does the very same: convert whatever you give to it into
a string. Whatever you do with that string is then up to you.
[color=blue]
> I am reading a C++ book, and I was wondering why
> the author never described how to concat vars (aside from the insertion
> operator)... That seems weird.[/color]
It's very simple: First convert everything to a std::string, then concat
all the individual pieces.
--
Karl Heinz Buchegger
kbuchegg@gascad.at