Thread: Printing colored text in code::blocks C++

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    6

    Printing colored text in code::blocks C++

    I have read where using system("Color xy") is not wise... but how do I print a line in color without printing the whole console in color?

    I am using Code::Blocks IDE...
    For example....

    cout << "This is some normal text" << endl;
    //I would like the next line to print in green...
    cout << "This is some text printed in green" << endl;
    //and reset color back to white text on black background

    Any help appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Since you seem to be using windows, use this.
    SetConsoleTextAttribute function - Windows Console | Microsoft Docs
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    6
    Thanks for your help!!

    Took me a bit to work out the console calls.... but it works!!
    I found a chart with the numbers for colors and used it...
    Here's my final code....

    HANDLE Con;
    Con = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute (Con, 2);
    cout << "test of colors this is green ----------------" << endl;
    Con = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute (Con,7);
    cout << "test of colors this is white ----------------" << endl;

    I feel fortunate to have found this forum. It's great!
    Thanks again!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by xnman View Post
    SetConsoleTextAttribute (Con, 2);
    You don't need to know that 2 is green.
    There are symbols to tell you that.

    Console Screen Buffers - Windows Console | Microsoft Docs
    Like
    SetConsoleTextAttribute (Con, FOREGROUND_GREEN);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    You can also use ANSI codes on Windows 10+ terminal (I don't know if it is available for older versions):

    Code:
    void EnableANSI( void  )
    { 
      HANDLE h;
      DWORD mode;
    
      h = GetStdHandle( STD_OUTPUT_HANDLE );
      GetConsoleMode( h, &mode );
      SetConsoleMode( h, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING );
    }
    Code:
    ...
    EnableANSI();
    
    puts( "\033[1;31mH\033[1;32mE\033[1;33mL\033[1;34mL\033[1;35mO\e[m" );
    ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Colored Text
    By P4r4digm in forum C Programming
    Replies: 5
    Last Post: 01-22-2007, 05:04 PM
  2. Printing a colored table , how to ?
    By GSalah in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2006, 07:59 AM
  3. Printing colored blocks in C
    By acid45 in forum C Programming
    Replies: 1
    Last Post: 05-05-2003, 01:23 PM
  4. Having Colored text
    By unknownUser in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 01:38 PM

Tags for this Thread