Click here to Skip to main content
11,934,809 members (49,610 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


88 bookmarked

Add Color to Console Projects

, 14 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
With the use of a few support functions, color can be added to console applications running under Win32.

Sample Image - maximum width is 600 pixels


The standard C++ iostream library does not know about colors. Hence, console applications tend to look very boring and often fail to emphasize the important bits in the flood of text. As I normally use console applications to do unit testing, I wanted the input conditions and the results of the test cases to stand out and be easily interpreted (i.e. PASSED in green and FAIL in red).

The Win32 API has some functions to manipulate the color of characters in a console but these are C style functions that do not interface seemlessly with the C++ stream style programming. Moreover, I want to keep my main code clean and uncluttered. Anybody reading my test cases should not have to wade through dozens of lines of output formatting code.

To that end, I created a handy set of iostream manipulators that allow me to change background and foreground colors at any point in the output stream.

Using the Code

All the necessary code is contained in one header file: Console.h. To guard against name clashing, the functions in this file are placed in a single namespace: "JadedHoboConsole".

As the following snippet shows, the use of the iostream color manipulators is simplicity itself.

#include "Console.h"

namespace con = JadedHoboConsole;

int main()
    using std::cout;
    using std::endl;

    cout << "I like " << con::fg_green << "green" << con::fg_white << " eggs and ham." 
         << endl;

The header file has stream manipulators to set background colors (those are the ones starting with bg_) and to set foreground colors (the ones prefixed with fg_). Additionally, I created a manipulator to clear the screen (clr).

This is the list of available manipulators:

  • fg_black
  • fg_gray
  • fg_white
  • fg_reg
  • fg_green
  • fg_blue
  • fg_cyan
  • fg_magenta
  • fg_yellow
  • bg_black
  • bg_gray
  • bg_white
  • bg_reg
  • bg_green
  • bg_blue
  • bg_cyan
  • bg_magenta
  • bg_yellow
  • clr

Points of Interest

New iostream manipulators are easy to implement because the most important part has already been done by the standardization committee when they included the basic_ostream& operator<<( basic_ostream& (*pf)(basic_ostream&)); overload in the standard. Thus, any function with the correct signature can be used as an stream manipulator:

std::ostream& Copyleft( std::ostream& os )
    os << "(L)2004 by EgoTripper";

cout << Copyleft << endl;


  • December 2004: First published
  • November 2009: Article updated


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jaded Hobo
Software Developer (Senior)
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Pranit Kothari21-May-12 19:25
memberPranit Kothari21-May-12 19:25 
Questionhow to use as argument in a fonction Pin
SublimeDeath9-Mar-11 14:50
memberSublimeDeath9-Mar-11 14:50 
AnswerRe: how to use as argument in a fonction Pin
Jaded Hobo10-Mar-11 11:01
memberJaded Hobo10-Mar-11 11:01 
GeneralRe: how to use as argument in a fonction Pin
SublimeDeath11-Mar-11 17:50
memberSublimeDeath11-Mar-11 17:50 
GeneralRe: how to use as argument in a fonction Pin
SublimeDeath11-Mar-11 18:09
memberSublimeDeath11-Mar-11 18:09 
GeneralRe: how to use as argument in a fonction Pin
Jaded Hobo12-Mar-11 10:00
memberJaded Hobo12-Mar-11 10:00 
GeneralRe: how to use as argument in a fonction Pin
SublimeDeath12-Mar-11 11:59
memberSublimeDeath12-Mar-11 11:59 
QuestionEasy to write Tcl wrapper ? Pin
klausnrooster3-Jul-10 23:15
memberklausnrooster3-Jul-10 23:15 
AnswerRe: Easy to write Tcl wrapper ? Pin
Jaded Hobo4-Jul-10 11:04
memberJaded Hobo4-Jul-10 11:04 
GeneralErrors with Dev-C++ Pin
Adam Kadlec19-Nov-09 11:30
memberAdam Kadlec19-Nov-09 11:30 
GeneralRe: Errors with Dev-C++ Pin
Adam Kadlec19-Nov-09 14:30
memberAdam Kadlec19-Nov-09 14:30 
GeneralRe: Errors with Dev-C++ Pin
Jaded Hobo25-Nov-09 10:00
memberJaded Hobo25-Nov-09 10:00 
GeneralGood, but... Pin
elbertlev4-Nov-09 14:04
memberelbertlev4-Nov-09 14:04 
GeneralRe: Good, but... Pin
Jaded Hobo11-Nov-09 9:25
memberJaded Hobo11-Nov-09 9:25 
GeneralRe: Good, but... Pin
Jaded Hobo17-Nov-09 9:18
memberJaded Hobo17-Nov-09 9:18 
GeneralThank you. Pin
Member 269292821-Mar-08 13:40
memberMember 269292821-Mar-08 13:40 
GeneralThanks a million Pin
spinelesspsycho22-Nov-07 7:11
memberspinelesspsycho22-Nov-07 7:11 
Questionclearscreen func? Pin
Member #38925979-Mar-07 23:45
memberMember #38925979-Mar-07 23:45 
AnswerRe: clearscreen func? Pin
Jaded Hobo20-Mar-07 12:05
memberJaded Hobo20-Mar-07 12:05 
AnswerRe: clearscreen func? Pin
Dathletez1327-Aug-07 22:46
memberDathletez1327-Aug-07 22:46 
GeneralUsing Dev C++ Pin
naMretupmoC22-Feb-07 10:46
membernaMretupmoC22-Feb-07 10:46 
GeneralAdding different color to stdout and stderr Pin
sachin ahuja30-Jan-07 22:57
membersachin ahuja30-Jan-07 22:57 
Hi All
Well I have used the following code to add color to stdout and stderr.
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdOut , &ConsoleScreenBufferInfoOutPut);

//Setting color for stdout
ConsoleScreenBufferInfoOutPut.wAttributes = FOREGROUND_GREEN;

SetConsoleTextAttribute(hStdOut ,ConsoleScreenBufferInfoOutPut.wAttributes);
It is working fine.
But when I used the same code with color red for stderr, I am getting the color for stdout as red though i have mentioned it as gree.
I have tried to use different structure for consolescreenbufferinfo but the problem is not solved.
Can anyone help in this regard.
AnswerRe: Adding different color to stdout and stderr Pin
Jaded Hobo4-Feb-07 7:15
memberJaded Hobo4-Feb-07 7:15 
GeneralWOW! Pin
JimmyDreak1-Dec-06 19:41
memberJimmyDreak1-Dec-06 19:41 
QuestionHow to get it? PURPURE! Pin
gravellpawepek15-Sep-06 3:12
membergravellpawepek15-Sep-06 3:12 
GeneralAmazing code, but... Pin
Narook15-Jul-06 7:18
memberNarook15-Jul-06 7:18 
GeneralError windows.h Pin
Muchailpski16-Jun-06 12:16
memberMuchailpski16-Jun-06 12:16 
GeneralRe: Error windows.h Pin
Da-Huntha25-Oct-07 14:30
memberDa-Huntha25-Oct-07 14:30 
GeneralExcellent :) Pin
peterxiau4-Jun-06 20:32
memberpeterxiau4-Jun-06 20:32 
GeneralGreat Job Pin
Molerat24-May-06 8:36
memberMolerat24-May-06 8:36 
GeneralRe: Great Job Pin
Molerat15-Jun-06 19:03
memberMolerat15-Jun-06 19:03 
GeneralAnother thing I noticed missing Pin
Sir Bastille30-Nov-05 7:13
memberSir Bastille30-Nov-05 7:13 
QuestionOther Colors? Pin
donaldGuy21-May-05 19:03
memberdonaldGuy21-May-05 19:03 
AnswerRe: Other Colors? Pin
Jaded Hobo24-May-05 8:18
memberJaded Hobo24-May-05 8:18 
GeneralAdding parameters Pin
nicchap4-Apr-05 12:25
membernicchap4-Apr-05 12:25 
GeneralRe: Adding parameters Pin
nicchap5-Apr-05 17:05
membernicchap5-Apr-05 17:05 
GeneralRe: Adding parameters [modified] Pin
RadekHa17-Jul-08 22:37
memberRadekHa17-Jul-08 22:37 
GeneralSmall correction needed Pin
Skyhawk29-Dec-04 15:29
memberSkyhawk29-Dec-04 15:29 
GeneralRe: Small correction needed Pin
Jaded Hobo30-Dec-04 6:48
memberJaded Hobo30-Dec-04 6:48 
GeneralGreat job Pin
WayneC29-Dec-04 1:36
memberWayneC29-Dec-04 1:36 
Generalgood Pin
spetsnaz28-Dec-04 12:03
memberspetsnaz28-Dec-04 12:03 
GeneralInteresting! Pin
WREY27-Dec-04 7:48
memberWREY27-Dec-04 7:48 
GeneralRe: Interesting! Pin
zghelp20-Nov-05 18:16
memberzghelp20-Nov-05 18:16 
GeneralPerfect! Pin
Dandy Cheung26-Dec-04 2:29
memberDandy Cheung26-Dec-04 2:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 14 Nov 2009
Article Copyright 2004 by Jaded Hobo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid