By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

int to char

P: n/a
wak
hi!
how can I convert an int to char !?

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


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
> hi!
how can I convert an int to char !?
I guess you intended to ask "how can I convert an int to a string"
string foo = "Hello";
for (int i=0 ; i <10 ; i++)
cout << foo.append(i);


http://www.parashift.com/c++-faq-lit....html#faq-38.1

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #2

P: n/a
wak wrote:
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
Jul 22 '05 #3

P: n/a

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
wak wrote:
hi!
how can I convert an int to char !?


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

Jul 22 '05 #4

P: n/a

"wak" <wa*@libero.it> wrote in message
news:63************************************@mygate .mailgate.org...
| 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
Jul 22 '05 #5

P: n/a
Jeremy Cowles wrote:

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
wak wrote:
hi!
how can I convert an int to char !?
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++?


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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

P: n/a

"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:5i********************@twister.tampabay.rr.co m...

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
wak wrote:
hi!
how can I convert an int to char !?


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


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.
Jul 22 '05 #7

P: n/a

"Chris ( Val )" <ch******@bigpond.com.au> wrote in message
news:br*************@ID-110726.news.uni-berlin.de...

"wak" <wa*@libero.it> wrote in message
news:63************************************@mygate .mailgate.org...
| 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.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.