c++ colour text
To display colored text in C++, you can use escape sequences. Here is an example of how you can achieve this:
Step 1: Include the necessary header file.
#include <iostream>
Step 2: Use escape sequences to change the text color.
std::cout << "\033[31m"; // Set the text color to red
std::cout << "This text is in red." << std::endl;
std::cout << "\033[0m"; // Reset the text color to the default
Explanation:
- In this example, we include the <iostream>
header file to use the input/output stream objects and functions.
- To change the text color, we use the escape sequence \033[
followed by the desired color code.
- The color code 31
represents the color red. You can use different color codes for different colors.
- After setting the color, we use std::cout
to display the colored text.
- To reset the color back to the default, we use the escape sequence \033[0m
.
Please note that the use of escape sequences to display colored text is not standard and may not work on all systems or terminals. It is recommended to use a library like ncurses for more advanced text manipulation and color handling in C++.