0

I am creating a small currency converting program in C++ in Visual Studio 2013.

For a pound sign, I have created a char variable using the Windows-1252 code for £ symbol.

const char poundSign(156);

I need to find a way in which I can implement a symbol for €, euro symbol. When I run the program.

  • 2
    That is not ASCII. Did you mean [Windows-1252](http://en.wikipedia.org/wiki/Windows-1252)? – user1937198 Jan 21 '15 at 18:42
  • code-pages are the worst thing that happened (for non-americans of course). Use Unicode. Preferably utf8. I know c++ standard support for utf8 is abysmal, but there are options like [ICU](http://site.icu-project.org/) and [Boost.Locale](http://www.boost.org/doc/libs/1_51_0/libs/locale/doc/html/index.html) – bolov Jan 21 '15 at 19:08
  • Related: [this](http://stackoverflow.com/q/19987448/509868) question, which uses UTF-8 (may be better than UCS-16) – anatolyg Jan 21 '15 at 19:26

2 Answers2

1

You should not use any of the deprecated extended ASCII encodings as they are long obsolete nowadays. As user1937198 said, 156 is the character code of £ in the archaic Windows-1252* encoding. The appearance of non-ASCII characters in these encodings depends on the codepage selected by the user, which makes it impossible to mix characters from different codepages under such a scheme. Additionally, your users will be very confused if they pick the wrong codepage.

Consider using Unicode instead. Since you're on Windows, you should probably use UTF-16, in which case the correct way is to declare:

// make sure the source code is saved as UTF-16!
const wchar_t poundSign[] = L"£";
const wchar_t euroSign[]  = L"€";

In UTF-16, there's no guarantee that a single character will take only 1 character due to the surrogate mechanism, hence it's best to store "characters"* as strings.

Keep in mind that this means the rest of your program should switch to Unicode as well: use the "W" versions of the Windows API (the WCHAR-versions).

[*] In technical lingo, each "Unicode character" is referred to as a code point.

Rufflewind
  • 8,545
  • 2
  • 35
  • 55
0

You have to distinguish between the translation environment and the execution environment. E.g., it heavily depends on where your program is being run what glyph is assigned to the character you intended to produce (cf. 'codepage').

You may want to check of your terminal or whatsoever is unicode-aware and then just use the appropriate codepoints.

Also, you should not create character constants the way you did. Use a character literal instead.

Kamajii
  • 1,826
  • 9
  • 21