Easy Tutorial: Computer Programming for DUMMIES (colored text on your terminal)

in #programming8 years ago (edited)

Hey guys! This is part 11 of my computer programming tutorials. This tutorial will be in C++. If you want to catch up, here are my other posts:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

Part 5: Loops

Part 6: Arrays

Part 7: Basic Input/Output

Part 9: Sorting Arrays

Part 10: Random Numbers

In this tutorial, I am going over how to print colored text to a Linux terminal. This will add a bit of life to your text-only programs! Note that not all Linux systems support this. All you have to do is insert a little bit of code into your cout statement. Example:

cout << "\033[1;32mLight green text!\033[0m\n";
  • This will print "Light green text!" in light green font.
    "\33" is the ESC character. When you follow this with an open bracket, two numbers with a semicolon in between, and an "m," this will change the font color. To change the color back to normal, insert "\033[0m" after the text. The values you can use for the first number are 0 and 1. The values for the second number are 30 - 37.

Here is a program I wrote to demonstrate all of the colors:

#include<iostream>
using namespace std;

int main()
{
    cout << "\033[1;30mDark Gray text!\033[0m\n";
    cout << "\033[1;31mLight red text!\033[0m\n";
    cout << "\033[1;32mLight green text!\033[0m\n";
    cout << "\033[1;33mYellow text!\033[0m\n";
    cout << "\033[1;34mLight blue text!\033[0m\n";
    cout << "\033[1;35mlight purple text!\033[0m\n";
    cout << "\033[1;36mLight cyan text!\033[0m\n";
    cout << "\033[1;37mWhite text... Boring...\033[0m\n";
    cout << "\033[0;30mBlack text!\033[0m\n";
    cout << "\033[0;31mRed text!\033[0m\n";
    cout << "\033[0;32mGreen text!\033[0m\n";
    cout << "\033[0;33mDark yellow text!\033[0m\n";
    cout << "\033[0;34mBlue text!\033[0m\n";
    cout << "\033[0;35mPurple text!\033[0m\n";
    cout << "\033[0;36mCyan text!\033[0m\n";
    cout << "\033[0;37mLight gray text!\033[0m\n";

    cout << "\033[1;32mLight green text!\n";
    cout << "Hello!\n";
    
    return 0;
}

Here is the output:

Notice how on the last line, I didn't set the color back to normal. This even turned my prompt green after the program was over!

I hope this was interesting! Leave suggestions in the comments!

Sort:  

Thanks for posting this. These are helpful for someone that's beginning in coding like me. I wish I seen more posts like these that help more.

Follow me, and I will post more! Please give as many suggestions as you want!

Oops, forgot to add that! It is in C++. For C, here is the equivalent:

#include<stdio.h>

int main()
{
    printf("\033[1;34mHello World!\033[0m\n");
    return 0;
}

cout function
C++ only -- include the 'iostream' library

printf() function
C or C++ -- include the 'stdio.h' library

This isn't helpful when writing an html document on Steemit.

Coin Marketplace

STEEM 0.20
TRX 0.12
JST 0.028
BTC 64182.86
ETH 3531.12
USDT 1.00
SBD 2.53