Cpp Display playing card suits

Some users were having problems previously with displaying playing card suits with cout. As of yet, I haven't been able to figure out how to do this with cout, but I've found a work around with wcout. see code below:

#include "pch.h"
#include <iostream>

#include <string>
#include <io.h>
#include <fcntl.h>

#define SPADE L"\u2660"
#define CLUB L"\u2663"
#define HEART L"\u2665"
#define DIAMOND L"\u2666"

enum SUIT {spade = 1, club, heart, diamond};

using namespace std;

void printSuit(int suitToSelect) {
_setmode(_fileno(stdout), _O_U16TEXT);
switch (suitToSelect) {
case spade:
wcout << SPADE;
break;
case club:
wcout << CLUB;
break;
case heart:
wcout << HEART;
break;
case diamond:
wcout << DIAMOND;
break;
}
_setmode(_fileno(stdout), _O_TEXT);
}

int main()
{

printSuit(spade);
printSuit(heart);
cout << "\n";
system("pause");
}

^the above printSuit function switches the entry mode to UTF16, outputs the symbol, and then returns the formatting to default for further cout use.

hoping someone will also pin this to the previously closed unsolved threads.

Hope this helps!

Cheers
- Kaz
Hmm there used to be symbols in the windows console for these, an alternate ascii mapping or something... I don't see them now but I know they used to be there.

you may have to keep using the unicode solution.
Another way is to use HSCD letters.
Last edited on
They are mapped to control characters.
3 → ♥
4 → ♦
5 → ♣
6 → ♠

You can still get them (on Windows) by holding down the Alt key and typing 3, 4, 5, or 6 on the numeric keypad, then releasing the Alt key.

However, control codes do not mix well with C++ I/O. Teh’s trick works because it uses Windows-specific code to switch the console to a half-baked UCS2 mode, then outputs the Unicode values for the symbols, AND he is using Microsoft’s compiler, which knows of such things and isn’t (completely) broken for wide character types.

Unicode on the Windows Console is actually a whole lot messier than this one example.
Last edited on
Topic archived. No new replies allowed.