Adding Colors to you C++ Program Output

Posted by Rey Dacoco in

There are several methods that can be used to add colors to your C++ program output but the simplest way is by using the system function. The system function allows you to pass commands to the operating system for execution. The syntax of using system varies in different programming languages. In C++, system function takes the following syntax:

system(“command”);
example:

system(“logoff”);
To add a color to your program output, use the color command which has the following syntax:

color bf
Were b is the background and f is the foreground.
Available colors for background and foreground are as follows:

0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White
So if you want to have a black background with light green text, use:

system(“color oa”);

system function is typed inside your main function (or inside other functions) just after the opening curly brace and before the closing curly brace.

example:
#include<iostream>
using namespace std;
int main()
{
system(“Color 0a”);
cout<<”I am blue”;
cin.get();
}

The codes above produce a red text in black background. There are several things that you can do with the system function, and it’s up to you to discover. Good luck!

0 comments

Post a Comment

Contributor

My photo
I am just a simple man with a simple mind. And this is my simple blog. New Posts will be available soon. Enjoy reading and never stop learning:)

Total Pageviews

Link List

Archives

Got questions? You can reach me here: