C++ doesn't really acknowledge that there is such a thing as a console. Because of this, I don't know of a standard way to change the color of console text across all platforms. I'd recommend installing a library called pdcurses in order to do things like fancy console I/O. http://pdcurses.sourceforge.net/
// Text Color.cpp : main project file.
#include <stdafx.h>
#include <stdio.h>
#include "conio.h"
#include <iostream>
#include <windows.h>
usingnamespace std;
/*
The different color codes are
0 BLACK
1 BLUE
2 GREEN
3 CYAN
4 RED
5 MAGENTA
6 BROWN
7 LIGHTGRAY
8 DARKGRAY
9 LIGHTBLUE
10 LIGHTGREEN
11 LIGHTCYAN
12 LIGHTRED
13 LIGHTMAGENTA
14 YELLOW
15 WHITE
*/
int main ( void )
{
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
WORD wOldColorAttrs;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
/*
* First save the current color information
*/
GetConsoleScreenBufferInfo(h, &csbiInfo);
wOldColorAttrs = csbiInfo.wAttributes;
/*
* Set the new color information
*/
SetConsoleTextAttribute ( h, FOREGROUND_RED & FOREGROUND_BLUE | BACKGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY );
printf ( "This is a test\n" );
/*
* Restore the original colors
*/
SetConsoleTextAttribute ( h, wOldColorAttrs);
return 0;
why I type
SetConsoleTextAttribute ( h, FOREGROUND_RED & FOREGROUND_BLUE );
printf("YA\n");
SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_BLUE );
printf("YA\n");
the former can't show anything but the latter can?