CPP output coloured text in console

deebuls

Deebul Nair

Posted on June 29, 2017

CPP output coloured text in console

Coloured Debug output

Print statements are the most widely used debug method for coding in any programming. Its always useful to have a bag of methods for good print statements for debugging.

One of the method is to color the output on the screen. Coloured debug statements are far more effective that plain white statements. For example can have green for positive statements and red for error statements.

In cpp there are tons of ways of doing things. I always like the KISS (keep it simple silly) method . The below code seems the most simplest way of doing it.

#include <iostream>
#include <string>


int main()
{
    const std::string red("\033[0;31m");
    const std::string green("\033[1;32m");
    const std::string yellow("\033[1;33m");
    const std::string cyan("\033[0;36m");
    const std::string magenta("\033[0;35m");
    const std::string reset("\033[0m");

    std::cout << yellow << " Hello color yellow " << reset << std::endl;
    std::cout << red << " Hello color red " << reset << std::endl;
    std::cout << green << " Hello color green " << reset << std::endl;
    std::cout << cyan <<" Hello color cyan " << reset << std::endl;
    std::cout << magenta << " Hello color magenta " << reset << std::endl;
     return 0;
}

Enter fullscreen mode Exit fullscreen mode

Obviously it has its drawbacks. I don't know now , but feel free to comment ✏️ of it doesn't work for you .

💖 💪 🙅 🚩
deebuls
Deebul Nair

Posted on June 29, 2017

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

1 chi dars
cpp 1 chi dars

August 19, 2024

c++ da 7-mavzu
undefined c++ da 7-mavzu

August 20, 2024

Arifmetik operatorlar | C++
cpp Arifmetik operatorlar | C++

March 12, 2024

setw() & setfill() | C++ boshlang'ich
beginners setw() & setfill() | C++ boshlang'ich

March 12, 2024