Ascii Characters in Overload Need Different Colors

I am including a small snippet from a program that was put together recently. I would like to finish it with color to the ascii characters I have included in the overload. How might I go about defining a color for one of these chars {D,C,H,S}? I am completely stumped at the moment; escape sequences won't work nor will anything else I tried on the internet, like the windows.h SetConsoleTextAttribute(). Please do not reply with some silly function that was found somewhere else on Google regarding my question, there has to be a much more simple approach/ solution than what is out there so far. Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 ostream& operator<<(ostream& os, const Cards& aCards)
{
	const string Z_CARD[]={"0","A","2","3","4","5","6","7","8","9","10","J","Q","K"};

	char D=4;
	char C=5;
	char H=3;
	char S=6;

    const char Z_SUIT[]={D,C,H,S};

	if(aCards.z_Up)
		os<<Z_CARD[aCards.z_Card]<<Z_SUIT[aCards.z_Suit];
	else
		os<<"??";
	return os;
}
Well I've only ever seen one way to have different colors in a console, but since you should be able to find the "silly" function on google I'm tempted not share it especially since its more work that you sound like you want to do. On the off chance it might help someone else... https://www.youtube.com/watch?v=IXtm7cI21vM
Please do not reply with some silly function that was found somewhere else on Google regarding my question, there has to be a much more simple approach/ solution than what is out there so far.

Why? Remember, C++ does not define that you are working with a console when you are using streams. It doesn't even define that the computer you are running your program on has to have a screen. This means that the only way to get colours is to go straight to the operating system, which defines that yes, you are working on a console, and yes, this computer has a screen (and gives you errors if those presumptions are true). As such, SetConsoleTextAttribute is the only way to do it on windows.

Well, if you don't want to use 'some silly function that was found somewhere else on google'. There is the "curses" libraries, I think that pdcurses is the one for Windows - they provide a bunch of functions for working with the console. Then there is an article on the website ( http://www.cplusplus.com/articles/Eyhv0pDG/ ) which provides an example implementation of how you might do it.

And then there is also conio.h as an absolute last resort. But please don't use that unless you have to - it is outdated and has a number of compatibility problems...
What do you mean by SetConsoleTextAttribute didn't worked ?didn't it compile ? did it produce unexpected output ?

Here is a simple working example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <windows.h>

struct Cards
{
    enum Suit {
        DIAMOND, CLUB, HEART, SPADE
    };

    enum Card {
        ZERO , // ?
        ACE,
        TWO,
        THREE,
        FOUR,
        FIVE,
        SIX ,
        SEVEN,
        EIGHT,
        NINE,
        TEN,
        JACK,
        QUEEN,
        KING
    };

    // ...
    Suit suit;
    Card card; // ?
    // ... etc..
};

std::ostream& operator<< ( std::ostream& os, const Cards& cards )
{
    const char* CARDS[] = {
        "0", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
    };

    const char* SUITS[] = {
        "D", "C", "H", "S"
    };

    const WORD COLORS[] = {
        // background -- foreground
        0x01, // Diamond -- Blue on Black
        0x02, // Club    -- Green
        0x03, // Heart   -- Aqua
        0x04  // Spade   -- Red
    };

    // Get handle to the console
    HANDLE hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );

    if( hStdOut ) { // if valid handle
        // change ConsoleTextAttribute
        SetConsoleTextAttribute( hStdOut, COLORS[cards.suit] );

        os << CARDS[cards.card] << SUITS[cards.suit];

        // set console color back to white on black
        SetConsoleTextAttribute( hStdOut, 0x08 );
    }

    return os;
}

int main()
{
    Cards cards[] = {
        { Cards::DIAMOND, Cards::ACE },
        { Cards::CLUB,    Cards::JACK }
    };

    std::cout << cards[0] << " " << cards[1] << std::endl;
}
Last edited on
Very nice nvrmnd, this works! Thank you!

In regards to what the others wrote: I do not mean to be persnickety, however I've noticed a lot of people try helping by posting a function or template that ends up creating more complications. Since the char suit [] is an internal data type, I figured there was a way to work with it without having to add a ton more code to the overload.
Topic archived. No new replies allowed.