1

I've been searching everywhere and can't believe how hard it is to find. I have the following code:

cout << "Hello";
cout << "Bye";

I want the first line to be green and the second line the be red. I managed to find:

system(" color 2");

However this makes all the text in the console green.

I only need it to work on Windows 7

  • 1
    I don't think there's a portable way of doing this. Bash uses colour codes (see http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html) for example. Other shells, especially on other OSes will do this differently. – arne Dec 16 '13 at 10:05
  • On Linux you can use Ansi color codes. Take a look at [this answer][1]. [1]: http://stackoverflow.com/questions/2353430/how-can-i-print-to-the-console-in-color-in-a-cross-platform-manner – Sean Dec 16 '13 at 10:06
  • Under DOS, you can also use ANSI colors if you add `device=C:\DOS\ANSI.SYS` to your `config.sys`. I prefer `devicehigh`, of course, to use all those spare UMBs. – Kerrek SB Dec 16 '13 at 10:10
  • I only need it to work with windows 7 –  Dec 16 '13 at 10:10
  • Try this : http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048382857&id=1043284392 – Yabada Dec 16 '13 at 10:11
  • If you only need it for Windows7 then use the Console API, see the SetConsoleTextAttribute() function from here: http://msdn.microsoft.com/en-us/library/ms682073.aspx – Duncan Smith Dec 16 '13 at 10:17

5 Answers5

4

Unfortunately. there's no native portable way to do this, as bash/linux will use ANSI escape codes (as did older DOS), but Windows XP or later will use the Console API (textcolor, etc) calls.

You might get a third party library that can provide a single API for both (ncurses/p-curses).

Have a look here

Community
  • 1
  • 1
Duncan Smith
  • 530
  • 2
  • 10
3

If you are using windows you can use SetConsoleTextAttribute to color the text to whatever collor:

void change_color(const int color_flags)
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute( hConsole, color_flags );
}
template <class T>
void print_colored(const int color_flags,const T & arg)
{
    change_color(color_flags);
    cout << arg;
    change_color(FOREGROUND_RED   | FOREGROUND_GREEN | FOREGROUND_BLUE);//back to normal
}

print_colored(FOREGROUND_RED,"stuff");//this will be red

You can find more info on the coloring flags here

Raxvan
  • 6,257
  • 2
  • 25
  • 46
2

Here a solution that works both in Windows and Linux:

#include <string>
#include <iostream>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
#endif // _WIN32

using namespace std;

#define color_black      0
#define color_dark_blue  1
#define color_dark_green 2
#define color_light_blue 3
#define color_dark_red   4
#define color_magenta    5
#define color_orange     6
#define color_light_gray 7
#define color_gray       8
#define color_blue       9
#define color_green     10
#define color_cyan      11
#define color_red       12
#define color_pink      13
#define color_yellow    14
#define color_white     15

void print(const string s="") {
    cout << s << endl;
}
void print(const string s, const int textcolor, const int backgroundcolor) {
#if defined(_WIN32)
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    WORD default_colors = 0;
    if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) default_colors = csbi.wAttributes;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), backgroundcolor<<4|textcolor);
#elif defined(__linux__)
    string t, b;
    switch(textcolor) {
        case  0: t="30"; break; // color_black      0
        case  1: t="34"; break; // color_dark_blue  1
        case  2: t="32"; break; // color_dark_green 2
        case  3: t="36"; break; // color_light_blue 3
        case  4: t="31"; break; // color_dark_red   4
        case  5: t="35"; break; // color_magenta    5
        case  6: t="33"; break; // color_orange     6
        case  7: t="37"; break; // color_light_gray 7
        case  8: t="90"; break; // color_gray       8
        case  9: t="94"; break; // color_blue       9
        case 10: t="92"; break; // color_green     10
        case 11: t="96"; break; // color_cyan      11
        case 12: t="91"; break; // color_red       12
        case 13: t="95"; break; // color_pink      13
        case 14: t="93"; break; // color_yellow    14
        case 15: t="97"; break; // color_white     15
        default: t="97";
    }
    switch(backgroundcolor) {
        case  0: b= "40"; break; // color_black      0
        case  1: b= "44"; break; // color_dark_blue  1
        case  2: b= "42"; break; // color_dark_green 2
        case  3: b= "46"; break; // color_light_blue 3
        case  4: b= "41"; break; // color_dark_red   4
        case  5: b= "45"; break; // color_magenta    5
        case  6: b= "43"; break; // color_orange     6
        case  7: b= "47"; break; // color_light_gray 7
        case  8: b="100"; break; // color_gray       8
        case  9: b="104"; break; // color_blue       9
        case 10: b="102"; break; // color_green     10
        case 11: b="106"; break; // color_cyan      11
        case 12: b="101"; break; // color_red       12
        case 13: b="105"; break; // color_pink      13
        case 14: b="103"; break; // color_yellow    14
        case 15: b="107"; break; // color_white     15
        default: b= "40";
    }
    cout << "\033["+t+";"+b+"m";
#endif // Windows/Linux
    cout << s;
#if defined(_WIN32)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_colors);
#elif defined(__linux__)
    cout << "\033[0m"; // reset color
#endif // Windows/Linux
}
void wait() {
    cin.get();
}

void print_color_test() {
    print(" ", color_black, color_magenta   );
    print(" ", color_black, color_blue      );
    print(" ", color_black, color_light_blue);
    print(" ", color_black, color_cyan      );
    print(" ", color_black, color_green     );
    print(" ", color_black, color_yellow    );
    print(" ", color_black, color_orange    );
    print(" ", color_black, color_red       );
    print();
    print(" ", color_black, color_black     );
    print(" ", color_black, color_gray      );
    print(" ", color_black, color_light_gray);
    print(" ", color_black, color_white     );
    print(" ", color_black, color_pink      );
    print(" ", color_black, color_dark_blue );
    print(" ", color_black, color_dark_green);
    print(" ", color_black, color_dark_red  );
    print();
    print("#", color_magenta   , color_black);
    print("#", color_blue      , color_black);
    print("#", color_light_blue, color_black);
    print("#", color_cyan      , color_black);
    print("#", color_green     , color_black);
    print("#", color_yellow    , color_black);
    print("#", color_orange    , color_black);
    print("#", color_red       , color_black);
    print();
    print("#", color_black     , color_black);
    print("#", color_gray      , color_black);
    print("#", color_light_gray, color_black);
    print("#", color_white     , color_black);
    print("#", color_pink      , color_black);
    print("#", color_dark_blue , color_black);
    print("#", color_dark_green, color_black);
    print("#", color_dark_red  , color_black);
    print();
}

int main() {
    print("test");
    print_color_test();
    print("test\n");

    print("Hello", color_green, color_black);
    print("Bye", color_red, color_black);

    wait();
    return 0;
}
ProjectPhysX
  • 4,535
  • 2
  • 14
  • 34
1

The approach which works for me is to use ANSI escape codes but I'm not using Windows (it may still work, though):

#include <iostream>

int main()
{
    std::cout << "\x1b[32;1mHello\x1b[0m\n";
    std::cout << "\x1b[31;1mBye\x1b[0m\n";
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
1

In c++ on an ANSI capable terminal, it is possible to write your own ansi stream manipulators like std::endl but for handling ansi escape code in a more readable way when used.

Code for doing so can look like this for basic raw implementation:

namespace ansi {
  template < class CharT, class Traits >
  constexpr
  std::basic_ostream< CharT, Traits > & reset( std::basic_ostream< CharT, Traits > &os )
  {
     return os << "\033[0m";
  }

  template < class CharT, class Traits >
  constexpr
  std::basic_ostream< CharT, Traits > & foreground_black( std::basic_ostream< CharT, Traits > &os )
  {
     return os << "\033[30m";
  }

  template < class CharT, class Traits >
  constexpr
  std::basic_ostream< CharT, Traits > & foreground_red( std::basic_ostream< CharT, Traits > &os )
  {
     return os << "\033[31m";
  }
  ...
 } // ansi

And it can be used in a code like this:

std::cout << ansi::foreground_red << "in red" << ansi::reset << std::endl;
user760453
  • 191
  • 2
  • 3