isalnum('£') confusion

This is a discussion on isalnum('£') confusion within the C++ Programming forums, part of the General Programming Boards category; When I call isalnum() with the pound (£) sign I get an error like this, Debug Assertion Failed! Program:... File: ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Bromsgrove, UK
    Posts
    7

    isalnum('£') confusion

    When I call isalnum() with the pound (£) sign I get an error like this,

    Debug Assertion Failed!

    Program:...
    File: isctype.c
    Line: 56

    Expression: (unsigned)(c + 1) <= 256

    Upon further investigation I discover that int('£') is -93, ah I think isalnum canít handle negative values?

    Then I try char(-93) which gives me a u with a punctuation mark over it.

    So to clear things up I try this,

    Code:
    for(int i = 0; i < 256; i++)
    	{
    		cout<<i<<" is "<<char(i)<<endl;
    	}
    By looking at the output £ should have a value of 156.

    I then try the same code but with 0 to -255, this gives me a value of -100 for the pound sign.

    Can anybody explain whatís going on because itís all got me seriously confused.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Location
    Bromsgrove, UK
    Posts
    7

    More info

    Something I have just discovered (which is very strange),

    cout<<'£';

    will produce ķ and not £ as you would have thought, whatís going on?

    I am using VC++ 2005 on a Windows XP SP2 platform, all OS options are set to UK English and the VC++ IDE is set to use the same language as windows.
    Last edited by Zero_Point; 05-02-2006 at 10:36 AM.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    AFAIK, The GB pound sign is not an ASCII character. you'll have to use wchar and unicode to get it to output in the Windows console.

    this outputs all ASCII and extended-ASCII characters to the console in W2K, and £ isn't there on mine:
    Code:
    #include <iostream>
    
    int main()
    {
        for(unsigned char i(0); i!=16; ++i)
        {
            for(unsigned char j(0); j!=16; ++j)
                std::cout << static_cast<char>(i*16 + j) << " ";
            std::cout << std::endl;
        }
        std::cin.get();
    }

    Edit : as for actually using isalpha, isalnum, etc with a wide char, I don't think it's possible, there might be a wide char version available in some 3rd party library, but I couldn't turn anything up in google.
    Last edited by Bench82; 05-02-2006 at 12:12 PM.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    From Dinkumware.com:
    isalnum
    int isalnum(int c);
    The function returns nonzero if c is any of:

    a b c d e f g h i j k l m n o p q r s t u v w x y z
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    0 1 2 3 4 5 6 7 8 9
    or any other locale-specific alphabetic character.
    You will notice that it does not include the full ASCII character set... no punctuation, etc.

    AFAIK, The GB pound sign is not an ASCII character
    Correct. The regular ASCII chart only goes from 0 - 127. 128 - 255 may be extended ASCII, but it is not fully standardized.
    Last edited by DougDbug; 05-02-2006 at 01:58 PM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Location
    Bromsgrove, UK
    Posts
    7
    I can get the pound sign to output in the windows console its 156 in the extended ASCII character set (at least the one I'm using, I have been reading that there are more versions).

    The problem is that if I type the pound sign in MS VS it actually outputs 163 which is ķ.

    Having looked around I have encountered a fair few lists of extended ASCII all of which put the value of the pound sign at 156.

    Coincidently, the character map in windows gives the hex value of the pound sign as 0x9c (156) and the Keystroke as Alt+0163. Thatís for both the DOS: US and DOS: Western Europe character sets.

    For the Windows Western char set character map has hex 0xa3 (163)

    OK in the writing of this reply I may have stumbled upon something. I would still appreciate ideas however.

    PS Thanks for the reply Bench82.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Your assertion failed because (unsigned)(-93 + 1) <= 256 is false.

    You can use L"unicode" to quote a unicode string, I think.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    At the risk of restarting old arguments, I think the ctype functions need to be cast:
    Code:
    isalnum( (unsigned char) ť);

  8. #8
    Registered User
    Join Date
    Apr 2006
    Location
    Bromsgrove, UK
    Posts
    7
    Yep, I can just use an unsigned char and it works fine as -93 is then 163.

    I would still like to sought out this thing where cout<<'£'; produces ķ on the command line though.

    I think windows is using Windows: Western character set and the console/cmd.exe is using DOS: US/Western Europe but canít see how to fix the problem simply i.e. without unicode (which I know nothing about) etc.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yes, the console uses the OEM codepage which is codepage 437 on US systems. You can fix this by calling:
    Code:
    SetConsoleCP(1252);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. for loop confusion
    By Enges in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2006, 08:21 AM
  4. Server-net newbie confusion
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2005, 02:08 AM
  5. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 02:45 PM

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