473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

int to char

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

"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

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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.