Hi. I try to change the text color of console in C++. I know some commands like .

system("color fg");

But I want to change the color of only one line of console. Is this possible? Would someone help me, please?

on Windows OS.

Thanks, nullptr!

A Windows console color table looks like this:

   Color             Background Foreground
    ---------------------------------------------
    Black            0           0
    Blue             1           1
    Green            2           2
    Cyan             3           3
    Red                  4           4
    Magenta          5           5
    Brown            6           6
    White            7           7
    Gray             -           8
    Intense Blue     -           9
    Intense Green    -           10
    Intense Cyan     -           11
    Intense Red          -           12
    Intense Magenta  -           13
    Yellow           -           14
    Intense White    -               15

To set background colors you have to combine the foreground color code with
the background color code using this equation:

finalcolor = (16*backgroundcolor) + foregroundcolor

if you want to set a text color that has a blue background and white text
you simply look up the color code in the table. Blue is 1 and white is 15;

Hence int backgroundcolor=1; and int foregroundcolor=15;

    #include <windows.h>
    #include <iostream> 
    using namespace std;

    void setcolor(int color)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
        return;
    }

    int main()
    {

        int foregroundcolor=15;
        int backgroundcolor=1;
        int finalcolor;

        finalcolor=(16*backgroundcolor)+foregroundcolor;

        setcolor(finalcolor);
        cout<<"finalcolor=(16*backgroundcolor)+foregroundcolor\n";
        setcolor(7);

        return 0;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.