Colorizing text in the console with C++

后端 未结 12 2579
礼貌的吻别
礼貌的吻别 2020-11-27 10:04

How can I write colored text to the console with C++? That is, how can I write different text with different colors?

12条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 11:00

    The simplest way you can do is:

    #include 
    
    system("Color F3");
    

    Where "F" is the code for the background color and 3 is the code for the text color.

    Mess around with it to see other color combinations:

    system("Color 1A");
    std::cout << "Hello, what is your name?" << std::endl;
    system("Color 3B");
    std::cout << "Hello, what is your name?" << std::endl;
    system("Color 4c");
    std::cout << "Hello, what is your name?" << std::endl;
    

    Note: I only tested on Windows. Works. As pointed out, this is not cross-platform, it will not work on Linux systems.

提交回复
热议问题