text color change

I am trying to have the "X" printed red and the "O" printed green but it is all green, before it all changes to red.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int i=0; i<64; i++)
    {
        if((i==7)||(i==15)||(i==23)||(i==31)||(i==39)||(i==47)||(i==55)||(i==63))
        {
            system("Color 4");
            std::cout<< "X"<<std::endl;
        }
        else
        {
            system("Color 2");
            std::cout << "O ";
        }

    }
That's because the color command changes everything to that color.
Sets the default console foreground and background colors.

COLOR [attr]

attr Specifies color attribute of console output

Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground. Each digit
can be any of the following values:

0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = White F = Bright White

If no argument is given, this command restores the color to what it was
when CMD.EXE started. This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
value.

The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
the COLOR command with a foreground and background color that are the
same.

Example: "COLOR fc" produces light red on bright white

You'll have to use some Windows API trickery if you want to make some things different colors from other things. (I'm not familiar with the code that does that off the top of my head)
as far as I know you cant use two different colors
1
2
3
4
void setColor(const DWORD& color) {
	const static HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(console, color);
}
Topic archived. No new replies allowed.