pocoa
pocoa

Reputation: 4337

Special Characters on Console

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.

Upvotes: 3

Views: 8150

Answers (3)

Jon Purdy
Jon Purdy

Reputation: 54971

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.

Upvotes: 0

Bounderby
Bounderby

Reputation: 525

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.

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308111

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.

Upvotes: 2

Related Questions