Thread: probably a really simple question, how to insert £?

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    50

    probably a really simple question, how to insert £?

    This seems such a simple problem but I've got no way to put the "£" into my code, all i get is the "ú" symbol?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not as simple as it seems. In a particularly impressive move of conflict between backwards compatibility and sane behaviour, the compatibility won, and so you end up with your compiler's editor using the Windows-1251 (most likely) character set while the console uses the IBM-DOS character set, codepage 443 or 850 or something like that. (443 is US, 850 is Germany, don't know what UK would be.)
    In effect, your compiler compiles the code, which is Win-1251, and takes the character codes of the string literally into the executable. There, in your exe, is a character code that would be the pound sign in Win-1251.
    However, when printing to the console, the same character code is interpreted as an 'ú' instead.

    Solution? Uh... I'll leave that to others. I think there's a setting somewhere in the console properties for that, but that's only a local solution.

    gcn_zelda: there's no character 156 in ASCII...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Of course, this depends on which font you have, or perhaps which code page you have for command line programs.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    This sounds a damn sight more compliacated then i thought it would be, if its any help I'm using microsofts visual c++ on windows xp pro using the british character set. Is there no way just insert a specific character like you can in HTML?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    if its any help I'm using microsofts visual c++ on windows xp pro using the british character set
    We guessed as much.

    Is there no way just insert a specific character like you can in HTML?
    Not really, no. Not a named character. It is possible to insert a specific character code, but that brings you back to the dependency.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    c++
    cout << (char)156;

    c
    printf("%c",156);
    Last edited by apsync; 01-05-2005 at 03:11 PM.

  7. #7
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Quote Originally Posted by CornedBee

    Not really, no. Not a named character. It is possible to insert a specific character code, but that brings you back to the dependency.
    dependency????

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Dependency on the character set that happens to be in use.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    c++
    char r = 156;
    cout << r;

    c
    printf("%c",156);
    I tried that as one of the first things, i just got a whole heap of errors chucked at me, plus we're not really allowed to use regular C code as that is what we've being taught up to now, I'm pretty sure that it has to stay as true to c++ as humanly possible.

    Dependency on the character set that happens to be in use.
    I'm not really bothered if it doesn't work on this system, what matters is if it works when compiled on a linux shell.

  10. #10
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    The fact that it works for you is irrelevant, since it may not work on others' computers.

    Edit : And why did you open up the whole std namespace for one cout? Just use std :: cout.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I'm not really bothered if it doesn't work on this system, what matters is if it works when compiled on a linux shell.
    In that case, find a text editor that saves the files in the ISO-8895-1 encoding (I believe that's Linux's standard set), save them with that, transfer over the the Linux box and it should work.

    Point in case is that if you're developing at one machine but want to run on another, you'll have to keep this stuff in mind - and unfortunately, I'm not aware of any proper way to find out about these problems at runtime. Hey, even Java doesn't manage it!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Well it wont help him much if it works for you but not for him will it??

    If you want a bad, non-portable, not sure to work and probably error-prone method you will need to find out what value that character has, This little program can help you with that
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	unsigned char c = 0;
    	for(int i=0; i<256; c++, i++)
    		cout << (int)c << ": " << c << endl;
    
    }
    Then you will manually have to chech all the characters that are printed out and when you find it you will know that on your particular system with your charset and everything £ has the value to the left.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Characters are problematic anyway. Case in point? Try running this program, compiled as C++ in MSVC++:
    Code:
    #include <cctype>
    #include <iostream>
    
    int main()
    {
      using namespace std;
      cout << isprint('ö') << endl;
    }
    So, what do you think? Is the German umlaut ö a printable character? The answer: the program crashes...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by CornedBee
    Characters are problematic anyway. Case in point? Try running this program, compiled as C++ in MSVC++:
    Code:
    #include <cctype>
    #include <iostream>
    
    int main()
    {
      using namespace std;
      cout << isprint('ö') << endl;
    }
    So, what do you think? Is the German umlaut ö a printable character? The answer: the program crashes...
    The valid range of arguments for isprint() goes from 0 through 127. In many compilers, the functions isprint(), isalnum(), etc. are implemented by indexing into an array of 128 chars. That doesn't mean that chars with values greater than 127 won't print, just that you had better not test them with isprint(), isalnum(), etc.

    Regards,

    Dave

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't know what the standard says, but MS's documentation on their implementation says this:
    The is routines produce meaningful results for any integer argument from –1 (EOF) to UCHAR_MAX (0xFF), inclusive. The expected argument type is int.

    Caution For the is routines, passing an argument of type char may yield unpredictable results. An SBCS or MBCS single-byte character of type char with a value greater than 0x7F is negative. If a char is passed, the compiler may convert the value to a signed int or a signed long. This value may be sign-extended by the compiler, with unexpected results.
    The confusing part about this is that not every single-character character constant is within that allowed range, because some of them are negative integer values. Yes, it is clearly warned about, but hey, this means that I can't even write a text in my native language without resorting to tricks!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM