954,224 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ASCII codes

I am doing a c++ assignment and just need a little help. I would just like to know how to get the ASCII code number after inputing an ASCII character. The user of this program should be able to input an uppercase letter and recieve the ASCII code value and its corresponding lower case letter with ASCII code value.

Thanks:icon_smile:

mstrmnd2008
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

just output the character as an integer, you might have to typecast it depending on what functions you use to display it.

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

> I would just like to know how to get the ASCII code number after inputing an ASCII character.
a. change the locale of std::cin from the default locale to one using an ASCII char set.
b. read the char entered by the user
c. convert it to an int to get the ascii code point.
d. use the tolower member of the ctype facet of the locale imbued by std::cin to get the lower case character
or check for the code point to be in the range for upper case chars; add 32 to get the lower case char (works for ascii)

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 
char c;
cin >> c;
// verify c is an upper case letter
c = c + 'a'-'A';
ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

>a. change the locale of std::cin from the default locale to one using an ASCII char set.
>b. read the char entered by the user
>c. convert it to an int to get the ascii code point.
>d. use the tolower member of the ctype facet of the locale imbued by std::cin to get the lower case character
Somehow I get the impression that the assignment isn't expecting an internationalized solution. :D

>// verify c is an upper case letter
>c = c + 'a'-'A';
For future reference, while this works with ASCII, UTF-8, and UTF-16 (the encodings you're most likely to be dealing with), it's not strictly portable across all character sets. So keep in mind that while this may be how your teacher wants you to complete the assignment, isupper/islower and toupper/tolower provide a more robust solution.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You