Program: Scrolling ticker-tape message using substrings


This program continually displays a scrolling message in the middle of your terminal window.




The code uses a for loop to repeatedly extract and display two portions of the message, using the substr function.

To find out more about string functions, take a look in the book - available directly on Amazon in the USA, UK, France, Germany, Spain, Italy and Canada.




To try out the program, 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  ticker.cpp


To compile from the command-line:


    g++ -o ticker ticker.cpp


To run from the command-line:


    ./ticker



Here's the code:

#include <iostream>    // To use the screen output - cout "channel out"
#include <unistd.h>    // To slow things down by parts of a second - usleep function
#include <sstream>     // To convert numbers to character strings when telling the screen where to put the cursor


using namespace std;  // Want to use short abbreviations... cout rather than std::cout

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

// These constants are special "escape characters" that tell your terminal to change colour or switch bold on/off

const string GREEN = "\e[32m";
const string BOLD = "\e[1m";


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

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 message = "GREET1NG$ EaRTHL1ng$! ++ ++ ++ WeLC0Me 2 GCC oN tHE Pi ++ ++ ++ ";


    // Find number of characters in message

    int len = message.length();

    cout << GREEN << BOLD;
   
    // Repeat the ticker-tape forever

    while ( true )
    {
        for ( int tick = 0; tick < len; tick++ )
        {
            // Center message on screen
            // Assumes 80 cols x 40 rows in terminal window

            positionCursor( 12, 40- len/2 );

            // Display left half of ticker-tape message

            cout << message.substr( tick, len-tick );

            // Display right half of ticker-tape message

            cout << message.substr( 0, tick );
           
            // Tell screen to update immediately

            cout << flush;

            // Brief pause for quarter of a second

            usleep( 250000 );

        }  // end of for loop
   
    }  // end of while block of statements


// end of main function


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