Program: Morse Code using switch-case decisions



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

This example translates text into Morse Code.




The program asks you to enter a message. It then examines each character in whatever you type, using a switch statement to decide whether or not to replace any letters with their equivalent Morse Code patterns.



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 morse.cpp


To compile from the command-line:


    g++ -o morse morse.cpp


To run from the command-line:


    ./morse




Here's the code:


#include <iostream>    // Program will be using keyboard and screen


using namespace std;    // Will be using standard identifiers string, cin, cout, endl


int main()
{
    // User types in some text from the keyboard

    cout << "Please type in some text:" << endl;
    string myText;
    getline( cin, myText );



    // Find out how long the text is
    int lengthOfMyText = myText.length();


    // Examine each character in the text
    char letterAtThisPos;


    for ( int position = 0;  position < lengthOfMyText;  position++ )
    {
        // Extract single character from the string

        letterAtThisPos = myText.at( position );
        cout << letterAtThisPos << " is ";


        // Convert character to lower-case - it may have been a capital
        letterAtThisPos = tolower ( letterAtThisPos );


        // If it is a letter of the alphabet, change to Morse code
        switch ( letterAtThisPos )
        {
            case 'a':    cout << ".-";       break;
            case 'b':    cout << "-...";     break;
            case 'c':    cout << "-.-.";     break;
            case 'd':    cout << "-..";      break;
            case 'e':    cout << ".";        break;
            case 'f':    cout << "..-.";     break;
            case 'g':    cout << "--.";      break;
            case 'h':    cout << "....";     break;
            case 'i':    cout << "..";       break;
            case 'j':    cout << ".---";     break;
            case 'k':    cout << "-.-";      break;
            case 'l':    cout << ".-..";     break;
            case 'm':    cout << "--";       break;
            case 'n':    cout << "-.";       break;
            case 'o':    cout << "---";      break;
            case 'p':    cout << "-.-.";     break;
            case 'q':    cout << "--.-";     break;
            case 'r':    cout << ".-.";      break;
            case 's':    cout << "...";      break;
            case 't':    cout << "-";        break;
            case 'u':    cout << "..-";      break;
            case 'v':    cout << "...-";     break;
            case 'w':    cout << ".--";      break;
            case 'x':    cout << "-..-";     break;
            case 'y':    cout << "-.--";     break;
            case 'z':    cout << "--..";     break;
            case ' ':    cout << "(gap)";    break;



            // This will be chosen if none of the cases above have been selected...
            default:     cout << "?";        break;


        }  // end of switch statement


        // Move on to next line after displaying morse for the letter
        cout << endl;




    }  // end of the for loop


    // Display final blank line
    cout << endl;


    return 0;
}


Find out more - buy the book on Amazon!



Further details in Chapter 6: Making decisions