cout<<"enter your character form the key board";
cout<<"your character "<<ch<<" in ascii code is "<<i;
@Elcric -- you should probably try not to use exit as a variable name. Its not a reserved word or anything. The police will not show up at your house, but it is a standard function in the cstdlib and you might mask the function or cause a moment of confusion when someone is looking over your code...
plus it would be more logical if you loop was "while(exit == false)" -- at least to me. if exit is true I want to leave, if exit is false I don't want to leave...
I like it when programming is a nice logical language that speaks to me.
Internally, there's no such thing as a character. Characters are stored as ASCII Numbers in memory.
It's precisely why you can treat characters as integers in a loop construct like this:
for(int i='A'; i < 'Z'; i++)
So all you need to do is typecast a character to an int to get its ASCII code or viceversa if you want the character equivalent of a number.
Character to ASCII:
char x = 'A';
If you're using printf(), you dont even need to typecast it. You just need to specify the output format to printf. So we simple ask printf() to output a character or number depending on what you want to do like this:
char c ='A';
int i = 70;
//Char to ASCII
printf("%c = %d",c,c);
//ASCII to char
printf("%d = %c",i,i);
Ahhh, the freedoms of coding. I do like printf() as a function when trying o get output into certain formats, whether it be hex, ascii or integer, it's always nice to just be able to throw a 0x%08x or a %c or %i Always enjoyable. I like Born2c0de's way
This post has been edited by IngeniousHax: 30 May 2009 - 11:44 AM