Selective coloured text

For a console app; I know you can use System("color XX"), but that changes all the text and I only want just a word or several lines of text to be coloured.
You should avoid the system function like the plague.

In regards to changing the color of the screen, I only know how to do it on Windows OS. The following code will allow you to change the color when you want:
1
2
3
4
5
#include <windows.h>

void SetConsoleColor(int textColor, int bgColor) {
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (textColor + (bgColor * 16)));
}


Use:
SetConsoleColor(7, 0); // Set's the console to default colors

Note: I believe 7 is the default value (which is a dark gray).
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
#define black			0
#define d_blue			1
#define green			2
#define turqoise		3
#define d_red			4
#define d_purple		5
#define d_yellow		6
#define gray			7
#define d_gray			8
#define blue			9
#define l_green			10
#define l_blue			11
#define red			12
#define p_purple		13
#define yellow			14
#define white			15

. . .

for (int i = 0; i < 16; i++)
{    CoutC( i );    cout << "Displaying different colours.\n";    cin.get();    }

. . .

void CoutC(int color)
{
   HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute(hCon,color);
}


I tried Volatile's method, and that works thanks.
Topic archived. No new replies allowed.