944,208 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5760
  • C++ RSS
Oct 11th, 2007
0

ASCII codes

Expand Post »
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
Last edited by mstrmnd2008; Oct 11th, 2007 at 10:32 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mstrmnd2008 is offline Offline
3 posts
since Oct 2007
Oct 11th, 2007
0

Re: ASCII codes

just output the character as an integer, you might have to typecast it depending on what functions you use to display it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005
Oct 12th, 2007
0

Re: ASCII codes

> 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)
Last edited by vijayan121; Oct 12th, 2007 at 2:21 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Oct 12th, 2007
0

Re: ASCII codes

C++ Syntax (Toggle Plain Text)
  1. char c;
  2. cin >> c;
  3. // verify c is an upper case letter
  4. c = c + 'a'-'A';
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Oct 12th, 2007
0

Re: ASCII codes

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

>// 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Issues with double linked list
Next Thread in C++ Forum Timeline: Game Programming for Slot machines





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC