3

I've finished my poker game but now I want to make it look a bit better with displaying Spades, Hearts, Diamonds and Clubs. I tried this answer: C++: Printing ASCII Heart and Diamonds With Platform Independent

Maybe stupid but I try:

cout << 'U+2662' << endl;

I don't know how to write it.

Community
  • 1
  • 1
pocoa
  • 4,197
  • 9
  • 37
  • 45

3 Answers3

2

To output a UTF-8 character, you need to encode it as hex bytes. I'll steal this link to fileinfo.com from an answer to the question you linked - if you jump to the UTF-8 representation, it says 0xE2 0x99 0xA5 and you can convert that to "\xE2\x99\xA5" as a string.

However I can't guarantee that your console will display UTF-8 so this answer might not help.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Thanks for the answer Mark. With the above string, it displays: ♥? I guess if I can find the right ones, it'll display correctly. – pocoa Apr 12 '10 at 17:37
  • I think you've just proven what I already suspected, the console doesn't use UTF-8. Did you try the code that was presented in your linked question? – Mark Ransom Apr 12 '10 at 18:01
0

If you are just trying to use the old ASCII control characters in the Windows console, you can use:

cout << (char) 3 << (char) 4 << (char) 5 << (char) 6 << endl;
//hearts, diamonds, clubs, spades

This isn't Unicode, and I assume it is not very portable. I don't really know of a portable solution to your question.

Bounderby
  • 525
  • 1
  • 6
  • 14
0

You need to use std::wcout and use UTF-16 by encoding as hex bytes (as Mark answers), but I can't guarantee that any of your characters will display correctly on Windows (before Vista?) because the Windows console was never really intended for this sort of thing. You can use wcout with Visual Studio and maybe Cygwin, but I don't think MinGW supports it. Make sure to use wide character literals: L"string".

Alternatively, you can use the preprocessor to supply the correct definitions of constants for each platform. I imagine that there will be at most three—ASCII on older platforms, UTF-8 on the more modern, and UTF-16 (with wcout) on newer Windows.

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
  • There should be an easy way, like Mark's example. – pocoa Apr 12 '10 at 17:42
  • My compiler does not run this code "wcout << "\xE2\x99\xA5" << endl", says "warning: unused variable 'wcout'". I'm using Eclipse with MinGW 5.1.6. – pocoa Apr 17 '10 at 05:15