test if character

This is a discussion on test if character within the C++ Programming forums, part of the General Programming Boards category; I was wondering if there was a way to test if a key the user pressed is a character....

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    test if character

    I was wondering if there was a way to test if a key the user pressed is a character.
    "Computers aren't intelligent, they only think they are."

    **infected by Blizzarddog**
    I am a signature virus. Please add me to your signature so that I may multiply

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Do you mean as in letters a-z? Look into the isalpha() function
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    All characters are stored as integer codes(a char type is actually an int), so you can test what character was entered by comparing it to the integer codes for the characters you are interested in.

    For instance, the characters a-z are stored as the integer codes 97-122(from an ASCII table). So, if you wanted to check whether the char entered was a lower case letter, you could do something like this:
    Code:
    char letter;
    cin>>letter;
    
    if(letter >= 97 && letter <= 122)
      cout<<"Found a lower case letter.";
    Last edited by 7stud; 12-14-2003 at 10:26 PM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    http://www.cppreference.com/ascii.html <= an ASCII chart... most likely your machine is using ASCII... but I would go with the isalpha(); function found in <cstring>... it's alot easier and better for readability
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating C/C++ Unit Test Cases
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2009, 08:29 PM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  5. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 06:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21