HOW CAN I CHANGE CONSOLE COLOR ???

/me covers ears from the shouting

As Moschops said, it depends on the operating system, on the C compiler and libraries you are using, and in some cases, the specific type of console you are working with. I'm going to guess that you are working with a DOS console running under Windows, possibly in a DOS emulator such as Dosbox. I'll further speculate that you are using Turbo C or Turbo C++, since hardly anyone working with modern tools is going to be interested in doing this. Is this a fair assumption?

By console color if you mean, the output of the code will be in different color then try this

file: color.h

#include <ostream>
namespace Color {
    enum Code {
        FG_BLACK  =  30,
        FG_RED    =  31,
        FG_GREEN  =  32,
        FG_YELLOW =  33,
        FG_BLUE   =  34,
        FG_MAGENTA=  35,
        FG_CYAN   =  36,
        FG_DEFAULT=  39,
        BG_RED    =  41,
        BG_GREEN  =  42,
        BG_BLUE   =  44,
        BG_DEFAULT=  49
    };
    class Modifier {
        Code my_color;
         public:
        Modifier(Code pCode) : my_color(pCode){}
        friend std::ostream&
            operator << (std::ostream& os, const Modifier& obj) {
                return os <<"\033["<<obj.my_color<<"m";
            }
    };
}

file: main.cpp

#include "color.h"
#include <iostream>
Color::Modifier f_red1(Color::FG_RED);
Color::Modifier f_def1(Color::FG_DEFAULT);
Color::Modifier f_green1(Color::FG_GREEN);
Color::Modifier f_blue1(Color::FG_BLUE);
Color::Modifier f_yellow1(Color::FG_YELLOW);
Color::Modifier f_magenta1(Color::FG_MAGENTA);
Color::Modifier f_cyan1(Color::FG_CYAN);
Color::Modifier b_def(Color::BG_DEFAULT);
Color::Modifier f_black(Color::FG_BLACK);
Color::Modifier b_green(Color::BG_GREEN);

int main(){
std::cout << f_red1 << "Hello World" << std::endl;
std::cout << b_green << f_black << "Hello World" << std::endl;

return 0;
}

If you run a Windows box, you can use this approach ...

// change text color in Windows console mode
// colors are 0=black 1=blue 2=green and so on to 15=white  
// colorattribute = foreground + background * 16
// to get red text on yellow use 4 + 14*16 = 228
// light red on yellow would be 12 + 14*16 = 236
// tested with Pelles C  (vegaseat)  

#include <stdio.h>
#include <windows.h>   // WinApi header

int main()
{
  HANDLE  hConsole;
    int k;

  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

  // you can loop k higher to see more color choices
  for(k = 1; k < 255; k++)
  {
    SetConsoleTextAttribute(hConsole, k);
    printf("%3d  %s\n", k, "I want to be nice today!");
  }

  getchar();  // wait
  return 0;
}
This article has been dead for over six months. Start a new discussion instead.