K
K
kentos2021-12-01 00:16:44
C++ / C#
kentos, 2021-12-01 00:16:44

How to change the functionality relative to the selected language?

I am writing a small console program that decodes from English and Russian to Morse code, and vice versa. Now the problem is that no matter what language I choose, it still decodes, that is, if you choose Russian and write in English, it still chooses how to fix it so that when choosing a language, you can only decode the language that chose

The code
using namespace std;
void choose_language(bool& is_russian_enabled)
{
    short code;
    cout << "Ïîæàëóéñòà, âûáåðèòå ÿçûê.\n1 - Rus, 2 - Eng: ";
    while (!(cin >> code) || (code < 0) || code > 2)
    {
        cin.clear();
        cin.ignore(32000, '\n');
        cout << "You've entered wrong num, try again: ";
        _getch();
        system("cls");
        cout << "Please, choose lang.\n1 - Rus, 2 - Eng: ";
    }
    if (code == 1)
    {
        is_russian_enabled = true;
    }
}
string charToMorse(char letter)
{
    switch (letter)
    {
    case 'a': case 'A':
        return ".-"; break;
    case 'b': case 'B':
        return "-..."; break;
    case 'c': case 'C':
        return "-.-."; break;
    case 'd': case 'D':
        return "-.."; break;
    case 'e': case 'E':
        return "."; break;
    case 'f': case 'F':
        return "..-."; break;
    case 'g': case 'G':
        return "--."; break;
    case 'h': case 'H':
        return "...."; break;
    case 'i': case 'I':
        return ".."; break;
    case 'j': case 'J':
        return ".---"; break;
    case 'k': case 'K':
        return "-.-"; break;
    case 'l': case 'L':
        return ".-.."; break;
    case 'm': case 'M':
        return "--"; break;
    case 'n': case 'N':
        return "-."; break;
    case 'o': case 'O':
        return "---"; break;
    case 'p': case 'P':
        return ".--."; break;
    case 'q': case 'Q':
        return "--.-"; break;
    case 'r': case 'R':
        return ".-."; break;
    case 's': case 'S':
        return "..."; break;
    case 't': case 'T':
        return "-"; break;
    case 'u': case 'U':
        return "..-"; break;
    case 'v': case 'V':
        return "...-"; break;
    case 'w': case 'W':
        return ".--"; break;
    case 'x': case 'X':
        return "-..-"; break;
    case 'y': case 'Y':
        return "-.--"; break;
    case 'z': case 'Z':
        return "--.."; break;
    case 'à': case 'À':
        return ".-"; break;
    case 'á': case 'Á':
        return "-..."; break;
    case 'â': case 'Â':
        return ".--"; break;
    case 'ã': case 'Ã':
        return "--."; break;
    case 'ä': case 'Ä':
        return "-.."; break;
    case 'å': case 'Å':
        return "."; break;
    case 'æ': case 'Æ':
        return "...-"; break;
    case 'ç': case 'Ç':
        return "--.."; break;
    case 'è': case 'È':
        return ".."; break;
    case 'é': case 'É':
        return ".---"; break;
    case 'ê': case 'Ê':
        return "-.-"; break;
    case 'ë': case 'Ë':
        return ".-.."; break;
    case 'ì': case 'Ì':
        return "--"; break;
    case 'í': case 'Í':
        return "-."; break;
    case 'î': case 'Î':
        return "---"; break;
    case 'ï': case 'Ï':
        return ".--."; break;
    case 'ð': case 'Ð':
        return ".-."; break;
    case 'ñ': case 'Ñ':
        return "..."; break;
    case 'ò': case 'Ò':
        return "-"; break;
    case 'ó': case 'Ó':
        return "..-"; break;
    case 'ô': case 'Ô':
        return "..-."; break;
    case 'õ': case 'Õ':
        return "...."; break;
    case 'ö': case 'Ö':
        return "-.-."; break;
    case '÷': case '×':
        return "---."; break;
    case 'ø': case 'Ø':
        return "----"; break;
    case 'ù': case 'Ù':
        return "--.-"; break;
    case 'ú': case 'Ú':
        return ".--.-."; break;
    case 'û': case 'Û':
        return "-.--"; break;
    case 'ü': case 'Ü':
        return "-..-"; break;
    case 'ý': case 'Ý':
        return "...-..."; break;
    case 'þ': case 'Þ':
        return "..--"; break;
    case 'ÿ': case 'ß':
        return ".-.-"; break;
    default:
        return " "; break;
    }
}
string morseToChar(string morse)
{

    if (morse == ".-")
    {
        return "a";
    }
    else if (morse == "-...")
    {
        return "b";
    }
    else if (morse == "-.-.")
    {
        return "c";
    }
    else if (morse == "-..")
    {
        return "d";
    }
    else if (morse == ".")
    {
        return "e";
    }
    else if (morse == "..-.")
    {
        return "f";
    }
    else if (morse == "--.")
    {
        return "g";
    }
    else if (morse == "....")
    {
        return "h";
    }
    else if (morse == "..")
    {
        return "i";
    }
    else if (morse == ".---")
    {
        return "j";
    }
    else if (morse == "-.-")
    {
        return "k";
    }
    else if (morse == ".-..")
    {
        return "l";
    }
    else if (morse == "--")
    {
        return "m";
    }
    else if (morse == "-.")
    {
        return "n";
    }
    else if (morse == "---")
    {
        return "o";
    }
    else if (morse == ".--.")
    {
        return "p";
    }
    else if (morse == "--.-")
    {
        return "q";
    }
    else if (morse == ".-.")
    {
        return "r";
    }
    else if (morse == "...")
    {
        return "s";
    }
    else if (morse == "-")
    {
        return "t";
    }
    else if (morse == "..-")
    {
        return "u";
    }
    else if (morse == "...-")
    {
        return "v";
    }
    else if (morse == ".--")
    {
        return "w";
    }
    else if (morse == "-..-")
    {
        return "x";
    }
    else if (morse == "-.--")
    {
        return "y";
    }
    else if (morse == "--..")
    {
        return "z";
    }
    else if (morse == ".-")
    {
        return "à";
    }
    else if (morse == "-...")
    {
        return "á";
    }
    else if (morse == ".--")
    {
        return "â";
    }
    else if (morse == "--.")
    {
        return "ã";
    }
    else if (morse == "-..")
    {
        return "ä";
    }
    else if (morse == ".")
    {
        return "å";
    }
    else if (morse == "...-")
    {
        return "æ";
    }
    else if (morse == "--..")
    {
        return "ç";
    }
    else if (morse == "..")
    {
        return "è";
    }
    else if (morse == ".---")
    {
        return "é";
    }
    else if (morse == "-.-")
    {
        return "ê";
    }
    else if (morse == ".-..")
    {
        return "ë";
    }
    else if (morse == "--")
    {
        return "ì";
    }
    else if (morse == "-.")
    {
        return "í";
    }
    else if (morse == "---")
    {
        return "î";
    }
    else if (morse == ".--.")
    {
        return "ï";
    }
    else if (morse == ".-.")
    {
        return "ð";
    }
    else if (morse == "...")
    {
        return "ñ";
    }
    else if (morse == "-")
    {
        return "ò";
    }
    else if (morse == "..-")
    {
        return "ó";
    }
    else if (morse == "..-.")
    {
        return "ô";
    }
    else if (morse == "....")
    {
        return "õ";
    }
    else if (morse == "-.-.")
    {
        return "ö";
    }
    else if (morse == "---.")
    {
        return "÷";
    }
    else if (morse == "----")
    {
        return "ø";
    }
    else if (morse == "--.-")
    {
        return "ù";
    }
    else if (morse == ".--.-.")
    {
        return "ú";
    }
    else if (morse == "-.--")
    {
        return "û";
    }
    else if (morse == "-..-")
    {
        return "ü";
    }
    else if (morse == "...-...")
    {
        return "ý";
    }
    else if (morse == "..--")
    {
        return "þ";
    }
    else if (morse == ".-.-")
    {
        return "ÿ";
    }

    else { return " "; }
}
int main()
{
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    bool is_russian_enabled = false;
    choose_language(is_russian_enabled);

    if (is_russian_enabled)
    {

        cout << "Êîíâåðòåð àçáóêè Ìîðçå" << endl;
    }
    else
    {
        cout << "Morse Code Converter" << endl;
    }
    ///  char str[20];
     // cin >> str; // ââîäèì ñòðîêó, èñïîëüçóÿ Êèðèëëèöó
     // cout << "\nâûâîä: " << str << endl; // ââûâîä ñòðîêè
    char key;
    bool loop = true;
    while (loop)
    {
        bool toMorse = true;
        string input;
        string output = "";
        int node = 0;
        string sub;
        cout << "Type word or morse code(using - and .): " << endl;
        cin.clear();
        cin.ignore(32000, '\n');
        getline(cin, input);
        if (input[1] == '.' || input[1] == '-')
        {
            toMorse = false;
        }
        else
        {
            toMorse = true;
        }
        if (toMorse)
        {
            for (unsigned int i = 0; i < input.length(); i++)
            {
                if (i != 0)
                {
                    output += " ";
                }
                output = output + charToMorse(input[i]);
            }
        }
        else {
            for (unsigned int i = 0; i <= input.length(); i++)
            {
                if (input[i] == ' ')
                {
                    sub = input.substr(node, i - node);
                    output += morseToChar(sub);
                    node = i + 1;
                }
                if (i == input.length() - 1)
                {
                    sub = input.substr(node, i + 1 - node);
                    output += morseToChar(sub);
                    node = i + 1;
                }
            }
        }
        cout << output << endl;
        cout << "Press r to restart or any key to exit" << endl;
        cin >> key;
        if (key != 'r')
        {
            loop = false;
        }
        cin.clear();
        //cin.ignore();
        input = "";
        cout << endl << endl;
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
calculator212, 2021-12-01
@calculator212

is_russian_enabled is used for you only to determine what text will be displayed at the beginning, this does not affect the translation itself. The problem is that in the charToMorse and morseToChar functions you compare in two alphabets, to solve this problem, either create a separate function rusToMorse and enToMorse or divide the function into 2 parts and choose depending on is_russian_enabled.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question