Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Printing ASCII Heart and Diamonds With Platform Independent

Tags:

c++

unicode

ascii

I'm developing a card playing game and would like to print out the symbol for hearts, diamonds, spades and clubs. My target platform will be Linux.

In Windows, I know how to print out these symbols. For example, to print out 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 alluded to, a heart symbol won't be printed in Linux. Is there a standard library that can be used to print out a symbol for hearts, diamonds, spades and clubs in both Linux and Windows? What I have been researching so far is looking at Unicode since it is my understanding this is universal.

like image 741
different Avatar asked Jan 19 '10 15:01

different


People also ask

How do you print a heart symbol in C++?

If you want to print a heart symbol on the terminal you need to print an actual unicode heart symbol (e.g. U+2764 ❤) and the terminal must be configured for unicode and use a font that has that symbol.

What keys do you use to make a heart?

Windows: Press Alt+3 on your keyboard to instantly type a heart symbol (must have Number Pad). Alternatively, press Windows key + Period (.) to bring up the emoji keyboard. Mac: Press Cmd + Ctrl + Space to select heart symbols from the emoji keyboard.


1 Answers

If you want a portable way, then you should use the Unicode code points (which have defined glyphs associated to them):

♠ 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

Remember that everything below character 32 in ASCII is a control character. They have a meaning associated with them and you don't have a guarantee of getting a glyph or a behavior there (even though most control characters to have glyphs, although they were never intended to be printable). Still, it's not a safe bet.

However, using Unicode needs proper font and encoding support which may or may not be a problem on UNIX-likes.

On Windows at least some of the above code points map to the ASCII control character glyphs you're outputting if the console is set to raster fonts (and therefore not supporting Unicode or anything else than the currently set OEM code page). This only applies to the black variants since the white ones have no equivalent.

like image 168
Joey Avatar answered Oct 28 '22 07:10

Joey