You may use ANSI escape sequences to add colored text on Windows 10 Command Prompt and macOS Terminal.
For newer versions of macOS, you shouldn't have to configure anything for them to work.
For Windows 10, you may need to enable the ANSI escape sequences since they are disabled by default. It can be enabled with the following registry modification (run from the command prompt as administrator):
reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000001 /f
#include <iostream> using namespace std; string CLS = "\033[2J\033[1;1H"; //clear screen string Default = "\033[0m"; // default gray & reset background string White = "\033[37;1m"; // bright white string Red = "\033[31;1m"; string Pink = "\x1B[38;5;213m"; string Green = "\033[32;1m"; string SeaGreen = "\x1B[38;5;158m"; string Yellow = "\033[33;1m"; string Navy = "\033[34;1m"; string Blue = "\x1B[38;5;117m"; string Cyan = "\033[36;1m"; string Purple = "\x1B[38;5;171m"; string Orange = "\x1B[38;5;208m"; string Coral = "\x1B[38;5;204m"; string WhiteOnRed = "\033[41;1m"; string WhiteOnGreen = "\033[42;1m"; string WhiteOnYellow = "\033[43;1m"; string WhiteOnBlue = "\033[44;1m"; string WhiteOnPurple = "\033[45;1m"; string WhiteOnCyan = "\033[46;1m"; int main() { cout << Default << "Default " << White << "White " << Red << "Red " << Pink << "Pink " << Green << "Green " << SeaGreen << "SeaGreen " << Yellow << "Yellow\n"; cout << Navy << "Navy " << Blue << "Blue " << Cyan << "Cyan " << Purple << "Purple " << Orange << "Orange " << Coral << "Coral\n"; cout << Default; // reset before printing inverse colors cout << WhiteOnRed<<" White on Red "<<WhiteOnGreen<<" White on Green "<<WhiteOnYellow<<" White on Yellow " << endl; cout << WhiteOnBlue<<" White on Blue "<<WhiteOnPurple<<" White on Purple "<<WhiteOnCyan<<" White on Cyan " << endl; cout << Default; // reset after printing inverse colors return 0; } |
Default
White Red
Pink
Green
SeaGreen Yellow Navy Blue Cyan Purple Orange Coral White on Red White on Green White on Yellow White on Blue White on Purple White on Cyan |
If you are doing a console program in Windows, then you can change the color of the text using the windows.h library. Below is a setcolor function you can use to change to some popular colors.
#include <iostream> #include <windows.h> using namespace std; void setcolor(string C) { HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); if (C=="gray" || C=="grey" || C=="default") SetConsoleTextAttribute(h, 7); else if (C=="blue") SetConsoleTextAttribute(h, 9); else if (C=="green") SetConsoleTextAttribute(h, 10); else if (C=="cyan") SetConsoleTextAttribute(h, 11); else if (C=="red") SetConsoleTextAttribute(h, 12); else if (C=="purple") SetConsoleTextAttribute(h, 13); else if (C=="yellow") SetConsoleTextAttribute(h, 14); else if (C=="white") SetConsoleTextAttribute(h, 15); } int main() { setcolor("default"); cout << "default\n"; setcolor("blue"); cout << "blue\n"; setcolor("green"); cout << "green\n"; setcolor("cyan"); cout << "cyan\n"; setcolor("red"); cout << "red\n"; setcolor("purple"); cout << "purple\n"; setcolor("yellow"); cout << "yellow\n"; setcolor("white"); cout << "white\n"; return 0; } |
default blue green cyan red purple yellow white |
If you are doing a console program in Code::Blocks using Windows, you can play a wav (not mp3) file as shown below. The cricket.wav file must be in the same folder as your exe - it is not embedded into the exe. You must first make a change in the Code::Blocks settings: Select Settings, Compiler, Linker settings. Under Other linker options, paste the following: -lwinmm
Windows Code::Blocks sound |
// Paste this under Settings->Compiler->Linker settings->Other linker options: -lwinmm #include <iostream> #include <windows.h> #include <mmsystem.h> using namespace std; int main() { // cricket.wav must be in the same folder at the cpp and exe mciSendString("open cricket.wav alias MY_SND3", NULL, 0, NULL); mciSendString("play MY_SND3", NULL, 0, NULL); system("pause"); // pause program because sound stops when program ends } |
Output |
You may move the cursor to an x,y location in a Windows console program using the gotoxy function below. The top left position is 0,0.
Windows cursor movement |
#include <iostream> #include <windows.h> using namespace std; void gotoxy(int x, int y) { COORD p = {x, y}; SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), p); } int main() { gotoxy(0,0); cout << "0,0"; gotoxy(40,0); cout << "40,0"; gotoxy(0,10); cout << "0,10"; gotoxy(40,10); cout << "40,10"; } |
Output |