Output color

I've seen a lot of people who need help with color output
so i wrote this program which should help


#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <string>
using namespace 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(){
int color = 8;
char c=' ';
int y = 2;
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle,color);
string bla[3]={"Will turn blue", "Will turn green", "Will turn red"};
cout << bla[0] << " " << bla[1] << " " << bla[2] << endl << "'B' for blue " << "'G' For green " << "'R' for red" << endl;
while((c = _getch())!= 13)
{
if(c == 'b')
{
color = 8;
SetConsoleTextAttribute(handle,color);
color = 1;
SetConsoleTextAttribute(handle,color);
system("cls");
cout << bla[0] << " ";
color = 8;
SetConsoleTextAttribute(handle,color);
cout << bla[1] << " " << bla[2] << endl;
cout << "'B' for blue " << "'G' For green " << "'R' for red" << endl;
color = 1;
SetConsoleTextAttribute(handle,color);
}

else if(c == 'g')
{
color = 8;
SetConsoleTextAttribute(handle,color);
color = 2;

system("cls");
cout << bla[0] << " ";
SetConsoleTextAttribute(handle,color);
cout << bla[1] << " ";
color = 8;
SetConsoleTextAttribute(handle,color);
cout << bla[2] << endl;
cout << "'B' for blue " << "'G' For green " << "'R' for red" << endl;

color = 2;
SetConsoleTextAttribute(handle,color);
}
else if(c == 'r')
{
color = 8;
SetConsoleTextAttribute(handle,color);
color = 4;
system("cls");
cout << bla[0] << " " << bla[1] << " ";
SetConsoleTextAttribute(handle,color);
cout << bla[2] << endl;
color = 8;
SetConsoleTextAttribute(handle,color);
cout << "'B' for blue " << "'G' For green " << "'R' for red" << endl;
color =4;
SetConsoleTextAttribute(handle,color);
}
}
double max=pow(5.0,5.0);


system("pause");
}
And this is platform dependent. i think :)
Do not use that integer constants as parameter to SetConsoleTextAttribute(), use Microsoft macros instead, as recommended.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v=vs.85).aspx#_win32_character_attributes
Topic archived. No new replies allowed.