Program: Rapidly cycle through a sequence of messages using different colours



STOP PRESS! The full book is now available DIRECTLY from Amazon with lower shipping costs!

This program repeatedly displays a sequence of greetings using a series of different colours. The program gets faster and faster until it reaches maximum speed.




You can change the greetings or the order of colours using the greeting and rainbow arrays.


You can also change the number of lines that are shown by altering the value of the MAX_LINES constant.


Select and copy the C++ source-code at the bottom of this post.


Paste into a text editor, such as Nano or Geany.

Then save the new file, ending in .cpp

I used  greetings.cpp


To compile from the command-line:


    g++ -o greetings greetings.cpp


To run from the command-line:


    ./greetings



Here's the code:


##include <iostream>    // To use the screen output
#include <cstdlib>     // Using random numbers
#include <unistd.h>    // To slow things down by parts of a second
#include <sstream>     // To convert numbers to character strings when telling the screen where to put the cursor

using namespace std;  // Short abbreviations for cout and endl

// -------------------------------

// These constants are special "escape sequences" that tell your terminal to change colour

const string NORM = "\e[0m";
const string RED = "\e[31m";
const string GREEN = "\e[32m";
const string BLUE = "\e[34m";
const string LIGHTBLUE = "\e[37m";
const string YELLOW = "\e[38;5;226m";


// -------------------------------

void positionCursor( int screenRow, int screenColumn )
{
    // This function sends the cursor to a particular row and column in your terminal window
    // ready to display something at that position.

    string columnString, rowString;

    // Convert the column number to a character string - uses a new stringstream each time, don't need to reset it after use...
    stringstream columnConverter;
    columnConverter << screenColumn;
    columnConverter >> columnString;


    // Convert the row number to a character string
    stringstream rowConverter;
    rowConverter << screenRow;
    rowConverter >> rowString;


    // Put the strings together to make an "escape sequence" that tells screen where to position the cursor
    cout << "\e[" + rowString + ";" + columnString + "H";
}


// -------------------------------

// Program begins execution here...

int main()
{
    string greeting[] = { "   H E L L O !   ", " B 0 n J 0 u R !", "    H 0 L A !   ", "  K0n'n1CHiWa!  " };


    string rainbow[] = { RED, YELLOW, BLUE, GREEN, NORM, LIGHTBLUE };
       
    const int NUM_GREETINGS = 4;
   
    const int NUM_COLOURS = 6;


    
const int MAX_LINES = 3;


    int currentColour, currentGreeting;
       
    for ( int timesDisplayed = 0; timesDisplayed < 200; timesDisplayed++ )
    {
        for ( int lineNum = 0; lineNum < MAX_LINES; lineNum++ )
        {
            positionCursor( 10 + lineNum*2, 32 );

           
            // Change the display colour           
            currentColour = ( timesDisplayed + lineNum*2 ) % NUM_COLOURS;
           
            cout << rainbow[currentColour];

           
            // Display the next greeting line           
            currentGreeting = ( timesDisplayed + lineNum ) % NUM_GREETINGS;
           
            cout << greeting[currentGreeting];
           

            cout << endl;

        }  // end of lineNum for loop

        usleep( 200000 - ( timesDisplayed * 1000 ) );      

    }  // end of timesDisplayed for loop

    cout << NORM;
   
    positionCursor( 20, 32 );


   

    return 0;

}  // End of main function


Find out more - buy the book on Amazon...