C ++: ASCII printing of hearts and diamonds with an independent platform

I am developing a card game and want to print out the symbol of the heart, diamonds, spades and clubs. My target platform will be Linux.

On Windows, I know how to print these characters. For example, to print a heart (in ASCII), I wrote ...

// in Windows, print a ASCII Heart

#include <iostream>

using std::cout;
using std::endl;

int main()
{
 char foo = '\3';
 cout << heart << endl;
 system ( "PAUSE" );
 return 0;
}

However, as I mentioned, the heart symbol will not be printed on Linux. Is there a standard library that can be used to print a symbol for hearts, diamonds, spades and clubs on Linux and Windows? So far I have been studying Unicode, as I understand that this is universal.

Thanks for any help.

+5
source share
4 answers

, ( , ):

♠ U+2660 Black Spade Suit
♡ U+2661 White Heart Suit
♢ U+2662 White Diamond Suit
♣ U+2663 Black Club Suit
♤ U+2664 White Spade Suit
♥ U+2665 Black Heart Suit
♦ U+2666 Black Diamond Suit
♧ U+2667 White Club Suit

, 32 ASCII . , , ( , ). , .

Unicode , UNIX-like.

Windows ASCII, , (, , Unicode - , OEM-). , .

+12

Linux UTF-8 stdout, Unicode .

#include <iostream>

const char heart[] = "\xe2\x99\xa5";

int main() {
    std::cout << heart << '\n';
    return 0;
}

UTF-8 Unicode , fileformat.info ( "UTF-8 (hex)" ).

- . setlocale, . wchar_t char wcout cout.

#include <iostream>
#include <clocale>

const wchar_t heart[] = L"\u2665";

int main() {
    setlocale(LC_ALL, "");
    std::wcout << heart << L'\n';
    return 0;
}
+8

, , .

, , , , - .

, , Unicode Tables - ... Unicode, , .

Bottom line: it all depends on the font, and there is no guaranteed portable way to achieve it without bringing your own gylphs.

+1
source

The top answer is now outdated or incorrect.

ASCII codes for clubs, diamonds, hearts, blades are now '\5', '\4', '\3', '\6'respectively.

+1
source

All Articles