Translating numbers into morse code and english text

  c++

I tried to add numbers for English text string array and morse code array but for English to more it only translated 2 numbers into morse when I typed 3 numbers in English text. For morse to English text translation, the numbers in morse translated to random symbols.

How can I solve this? please need help in advance!

#include <iostream> 
#include <string.h> 
#include <sstream>

using namespace std;

string convertToEnglish(string morse, string const morseCode[], string const alpha[]);
string convertToMorse(string english, string const morseCode[], string const alpha[]);

int main()
{

string option = ""; 
cout << "TEXT TO MORSE CODE or MORSE CODE TO TEXT Please selection option: nSelect 1 to decode 
English text to Morse code nSelect 2 to decode Morse code to English text n";
getline(cin, option);

string const morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",".----","..---","...--"," "," "}; 

string const englishLetter[] = {"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","1","2","3",""," "}; 


if (option == "1") 
{
    //english to morse

    string input = ""; 
    cout << "Enter English words to translate, with a space between letters and three spaces between 
    words:";
    getline(cin, input);

    //Convert string to be all upper case
    string uppercaseInput = "";
    for (int i = 0; i < input.size(); i++)
    {
        uppercaseInput += toupper(input[i]);
    }

    cout << convertToMorse(uppercaseInput, morseCode, englishLetter) << endl;

} 
else if (option == "2")
{
    //morse to english
    string input = "";
    cout << "Enter morse with only space between code: n";
    getline(cin, input);
    cout << convertToEnglish(input, morseCode, englishLetter) << endl;
}



return 0;
}

string convertToMorse(string english, string const morseCode[], string const alpha[]) 
{

string output = "";
string currentLetter = "";

int position = 0;

int const letterCount = english.length(); 

int const numberOfAlpha = 28;

cout << "n";

while (position <= letterCount)
{
    currentLetter = string(1, english[position]); 

    int marker = 0;

    string addition = "";

    while (marker < 28)
    {           
        if (currentLetter == alpha[marker])
        {
            addition = morseCode[marker];
        }

        ++marker;
    }

    output += addition + " ";

    ++position;
    }


return output;

}

string convertToEnglish(string morse, string const morseCode[], string const alpha[]) 
{

string output = "";
string currentLetter = "";
istringstream ss(morse);    

int const characters = 28;

while(ss >> currentLetter)
{

    int index = 0;
    while(currentLetter != morseCode[index] && index < characters)
    {
        ++index; //increment here so we don't have to decrement after the loop like if we put in the condition
    }

    output += 'A' + index;
}

return output;
} 

Source: Windows Questions C++

LEAVE A COMMENT