int to char

W

wak

hi!
how can I convert an int to char !?

string foo = "Hello";
for (int i=0 ; i <10 ; i++)
cout << foo.append(i);
 
C

Christoph Rabel

wak said:
hi!
how can I convert an int to char !?

You can use a stringstream or sprintf.
string foo = "Hello";
for (int i=0 ; i <10 ; i++)
cout << foo.append(i);

cout << foo << i;

should work in this case. (It doesnt the same what you have written but
probably what you want)

In case you really want the above behaviour, you could, in this example
do it the following way:

for (int i=0 ; i <10 ; i++)
{
foo += '0' + i;
cout << foo;
}

Please be advised, that it only works with ints in the range 0-9 and
that C++98 does not guarantee that the letters 0-9 are in this order.

But C guarantees it and the C++2003 standard. (Please correct me if Im
wrong in this) So it should be fairly safe to use.

hth

Christoph
 
J

Jeremy Cowles

Christoph Rabel said:
You can use a stringstream or sprintf.

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++? 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.

TIA,
Jeremy
 
C

Chris \( Val \)

| hi!
| how can I convert an int to char !?
|
| string foo = "Hello";
| for (int i=0 ; i <10 ; i++)
| cout << foo.append(i);

static_cast<char>( i )

Cheers.
Chris Val
 
K

Karl Heinz Buchegger

Jeremy said:
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++?

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::eek:stringstream 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.
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.

It's very simple: First convert everything to a std::string, then concat
all the individual pieces.
 
J

jeffc

Jeremy Cowles said:
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++?

You can't do that in C++ because you're using "int", which is a built in
type from the C days. It's not a class. You could easily write one of
course, but your code wouldn't be portable.
 
J

jeffc

Chris ( Val ) said:
| hi!
| how can I convert an int to char !?
|
| string foo = "Hello";
| for (int i=0 ; i <10 ; i++)
| cout << foo.append(i);

static_cast<char>( i )

That wasn't very nice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top