+ 3
Is there a function in C++ to print text in particular colour?
11 Respuestas
+ 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...
+ 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...
+ 8
@Kinshuk: Unless thier intent it to make a program for Dos 5.x that is 😅
+ 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++'...
+ 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.
+ 7
I always recommend to not use this compiler.
+ 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.
+ 4
In turbo compiler
You can use
cprintf()
+ 4
This is for who still using turbo compiler.
There are so many people still using turbo compiler
+ 3
I use code::blocks for windows thank you very much!
+ 3
Welcome ^_^