Is there a function in C++ to print text in particular colour? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Is there a function in C++ to print text in particular colour?

15th Jun 2017, 12:24 PM
Shashikant Kadam
Shashikant Kadam - avatar
11 Answers
+ 9
If you're using Code::Blocks or Visual Studio in a Windows or Linux System, yes... Else, No... WINDOWS : --------- --------- --------- --------- --------- --------- In windows, you can use this custom function for ease: #include<windows.h> void SetConsoleColor(WORD COLOR) { HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(out,COLOR); } main() { SetConsoleColor(FOREGROUND_RED| FOREGROUND_INTENSITY); //Sets color to red! cout<<"Hello!"; } There are 8 Basic Colors available in C++, FOREGROUND_RED, FOREGROUND_BLUE, FOREGROUND_GREEN, FOREGROUND_INTENSITY, //controls shade, dark if not used... BACKGROUND_RED, BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_INTENSITY. You may use these in combination using | to create yellow, brown, etc, etc... Also, check this : https://code.sololearn.com/caDG2co3to6W/?ref=app Here you can find some other useful functions for windows and some easy defines to use as colors. Eg - Now you can do - SetConsoleColor(RED), instead of FOREGROUND_RED... Note that these change the color of the entire console for the particular moment, so the code above has a function SetConsoleOriginalColor()... Also, You will need to call GetDefault() before that to save the original state of the console...
15th Jun 2017, 3:01 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 8
LINUX : --------- --------- --------- --------- --------- --------- In linux, there are no special headers for ease of console control. To change colors, one may use system in cstdlib, or ANSI Escape codes for colors. (Neither work correctly in windows...) 0) system Use this like this: int main() { system("0x49");//May not work... cout<<"Hello"; } Check the number patterns randomly for getting familiar with the color codes, as no list exist for this... 1)ANSI Escape sequences They are of following format: "\033[#m", where # is to be replaced by a number,from 30 to 37, each representing a color... Use these like this: int main() { cout<< "\033[33m"<<"Hello"; //Prints hello in yellow! } These are some references for the list of colors and their codes: www.cplusplus.com/forum/unices/36461 www.misc.flogisoft.com/bash/tip_colors_and_formatting Hope this helps...
15th Jun 2017, 3:01 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 8
@Kinshuk: Unless thier intent it to make a program for Dos 5.x that is 😅
22nd Jun 2017, 3:00 AM
jay
jay - avatar
+ 7
@Martin Taylor Totally agree with you... One must stop using it at all, after all its just so, obsolete... @Keep in Mind Many people still use Turbo C++, but it is our duty to make them stop doing so. After all, why would you still use Windows NT in 2017 when you have Windows 10? This compiler, Turbo C++, is only promoted in a few developing countries, mostly India. It lacks the namespaces, template support, stl libraries C++ 11,14 and 17 support... The code produced in it is 100% non - cross platform, and you are restricted by boundaries. Thus its use must be stopped, as 'True C++' is totally different from 'Turbo C++'...
22nd Jun 2017, 2:50 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 7
@Kinshuk I am not using turbo but here so many learners still using. So I said that otherwise I don't suggest turbo. I always using code block.
22nd Jun 2017, 2:53 AM
Keep in mind 😎
Keep in mind 😎 - avatar
+ 7
I always recommend to not use this compiler.
22nd Jun 2017, 2:59 AM
Keep in mind 😎
Keep in mind 😎 - avatar
+ 6
@Keep in Mind I appreciate that you're not using Turbo C++. But you should also try to recommend not to use Turbo C++ to people still using it, whenever possible.
22nd Jun 2017, 2:55 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
In turbo compiler You can use cprintf()
22nd Jun 2017, 1:26 AM
Keep in mind 😎
Keep in mind 😎 - avatar
+ 4
This is for who still using turbo compiler. There are so many people still using turbo compiler
22nd Jun 2017, 1:45 AM
Keep in mind 😎
Keep in mind 😎 - avatar
+ 3
I use code::blocks for windows thank you very much!
15th Jun 2017, 3:03 PM
Shashikant Kadam
Shashikant Kadam - avatar
+ 3
Welcome ^_^
15th Jun 2017, 3:03 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar