Sebastian Sebastian - 1 month ago 17
C++ Question

English to Morse Converter

I've been working on a program that is supposed to convert english to morse code. I am having a really hard time dealing with the strings. For example I have no clue why I can have morseAlphabet have a set number of positions at [30] but I cannot do the same for latinAlphabet. Overall I have no clue how I am supposed to translate the words.

My idea was to see what character in the alphabet shows up in the first position of the phrase to be translated then print the corresponding alphabet position for the morse alphabet then move onto the second position in the phrase however me messing around with for loops just ended with me getting errors about for loops getting far too huge and memory errors or just gave me a blank.

With what I have right now whenever I enter the phrase to be translated it comes stops with a subscript out of range error and some of my earlier fiddling had it return jibberish(memory locations?) and I am really just out of ideas. I hope that this is phrased right and someone can help me because the past four hours of internet searches hasn't really helped me and to be honest at this point I am doubting if any of the stuff I've written is any use at all.

#include <iostream>
#include <string>


int main()
{
int operatingMode = 0;
using namespace std;
std::string latinPhrase;
std::string morsePhrase;
std::string latinAlphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',' };
std::string morseAlphabet[30] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".-.-.-", "--..--" };
std::string translatedMorsePhrase;
int wordSearch = 0;


std::cout << "Please select a mode of operation. " << endl;
std::cout << "Input 1 for English to Morse and 2 for Morse to English. " << endl;

std::cin >> operatingMode;

std::cout << "Your mode of operation is " << operatingMode << endl;

if (operatingMode == 1)
{
std::cout << "You have selected English to Morse." << endl;
std::cout << "Please enter the phrase you would like translated." << endl;

std::cin.ignore();
std::getline(std::cin, latinPhrase);
}

for (int counter = 0; counter < 30; counter++)
{
for (unsigned i = 0; i<latinPhrase.length(); ++i)
{
if (latinPhrase.at(i) == latinAlphabet[i])
{
cout << morseAlphabet[i];
}
}

std::cout << "The translated phrase is: " << translatedMorsePhrase << " stop" << endl;


return 0;
}

Answer

The bug in your current code is the erroneous use of i when you mean counter and incorrect iteration order. This should do the trick:

for (unsigned i = 0; i<latinPhrase.length(); ++i)
    {
        for (int counter = 0; counter < 30; counter++)
        {
                if (latinPhrase.at(i) == latinAlphabet[counter])
                {
                    cout << morseAlphabet[counter];
                    break;
                }
        }
    }

To answer your question about your trouble with declaring and initializing std::string latinAlphabet[30], you are trying to initialize an array of std::strings with a braced-init-list of chars {'A', 'B', ...}. Either change it to {"A", "B", ...} or change the type of latinAlphabet to char[30] and you will be able to say initialize it the way as an array.