English to Morse Converter

12.1k views Asked by At

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;
}
2

There are 2 answers

0
Pradhan On BEST 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.

0
chai-chai On
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <windows.h>
#include<fstream>
using namespace std;
int main()
{
//  Beep(523,700);
//  Beep(0,400);
//  Beep(523,200);
//  Beep(523,200);
//  Beep(523,200);
    char string[1000];
    cout << "Enter string : ";
    gets(string);

    int len;
    len=strlen(string);
    ofstream out("output.txt");
    ifstream in;
    for(int i=0; i<len; i++)
    {
        switch(string[i])
        {
            case 'a' :
            case 'A' :
                {
                    out << ".- ";
                    break;
                }
            case 'b' :
            case 'B' :
                {
                    out << "-... ";
                    break;
                }
            case 'c' :
            case 'C' :
                {
                    out << "-.-. ";
                    break;
                }
            case 'd' :
            case 'D' :
                {
                    out << "-.. ";
                    break;
                }   
            case 'e' :
            case 'E' :
                {
                    out << ". ";
                    break;
                }
            case 'f' :
            case 'F' :
                {
                    out << "..-. ";
                    break;
                }
            case 'g' :
            case 'G' :
                {
                    out << "--. ";
                    break;
                }
            case 'h' :
            case 'H' :
                {
                    out << ".... ";
                    break;
                }
            case 'i' :
            case 'I' :
                {
                    out << ".. ";
                    break;
                }   
            case 'j' :
            case 'J' :
                {
                    out << ".--- "; 
                    break;
                }
            case 'k' :
            case 'K' :
                {
                    out << "-.- ";
                    break;
                }
            case 'l' :
            case 'L' :
                {
                    out << ".-.. ";
                    break;
                }
            case 'm' :
            case 'M' :
                {
                    out << "-- ";
                    break;
                }
            case 'n' :
            case 'N' :
                {
                    out << "-. ";
                    break;
                }   
            case 'o' :
            case 'O' :
                {
                    out << "--- ";
                    break;
                }
            case 'p' :
            case 'P' :
                {
                    out << ".--. ";
                    break;
                }
            case 'q' :
            case 'Q' :
                {
                    out << "--.- ";
                    break;
                }
            case 'r' :
            case 'R' :
                {
                    out << ".-. ";
                    break;
                }
            case 's' :
            case 'S' :
                {
                    out << "... ";
                    break;
                }   
            case 't' :
            case 'T' :
                {
                    out << "- ";
                    break;
                }
            case 'u' :
            case 'U' :
                {
                    out << "..- ";
                    break;
                }
            case 'v' :
            case 'V' :
                {
                    out << "...- ";
                    break;
                }
            case 'w' :
            case 'W' :
                {
                    out << ".-- ";
                    break;
                }
            case 'x' :
            case 'X' :
                {
                    out << "-..- ";
                    break;
                }
            case 'y' :
            case 'Y' :
                {
                    out << "-.-- ";
                    break;
                }
            case 'z' :
            case 'Z' :
                {
                    out << "--.. ";
                    break;
                }
            case ' ' :
                {
                    out << " /  ";
                    break;
                }
            case '0' :
                {
                    out << "----- ";
                    break;
                }
            case '1' :
                {
                    out << ".---- ";
                    break;
                }
            case '2' :
                {
                    out << "..--- ";
                    break;
                }
            case '3' :
                {
                    out << "...-- ";
                    break;
                }
            case '4' :
                {
                    out << "....- ";
                    break;
                }
            case '5' :
                {
                    out << "..... ";
                    break;
                }
            case '6' :
                {
                    out << "-.... ";
                    break;
                }
            case '7' :
                {
                    out << "--... ";
                    break;
                }
            case '8' :
                {
                    out << "---.. ";
                    break;
                }
            case '9' :
                {
                    out << "----. ";
                    break;
                }



        }


    }
        out.close();
        char ch;
        in.open("output.txt");
        while(!in.eof())
        {
            in>>ch;
            if(ch=='.')
            {
                Beep(523,200);
            }
            else if(ch=='-')
            {
                Beep(523,700);
            }
            else if(ch==' ')
            {
                Beep(0,100);
            }
            else if(ch == '/')
            {
                Beep(0,400);
            }
        }
        in.close();

}