c++ terminal color

To change the text color in a C++ terminal, you can use the following steps:

  1. Include the necessary headers:
#include <iostream>
#include <windows.h> // for Windows
  1. Define the function to change the text color:
void SetColor(int color) {
    #ifdef _WIN32
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
    #else
        std::cout << "\033[" << color << "m";
    #endif
}
  1. Use the function to set the text color:
int main() {
    SetColor(12); // Example: setting the text color to red
    std::cout << "This text will be displayed in red." << std::endl;
    SetColor(7); // Resetting the text color to default
    return 0;
}