Morse to english translator with parameters asked

Write your question here.

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

/* This program translates morse code to english */
/* To close the program either terminate it manually or type a wrong input */

using namespace std;

int main(){

char alpha[37] = { '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','4','5','6','7','8','9','0',' '};

string morarr[37] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-", ".--", "-..-", "-.--","--..",".----","..---","...--","....-",".....","-....","--...","---..","----.","-----"," " };

char input;
int i;
string morse;
cout << "Insert morsecode to translate it ( c):\n";

/* This program check every character individually with cin.get() */
cin.get(input);
while(!cin.eof()){
/* Check if character isa good input */

if (input!= ' ' && input!= '-' && input!= '.' && input!= '\n'){
cout << "(Wrong input)";
return 0;
}
/* It checks if input is good ".--" and then morse is one letter of MORSECODE */
while ( (input != ' ' && input !='\n') && (input=='-'||input == '.')){

morse= morse+ input;
cin.get(input);

}
/*Here it checks If the MOESECODE we found above is one letter from the array with the alphabet */
for(i=0;i<37;i++){
if( morse==morarr[i]){
cout << alpha[i] ;
}
}

/* From here it checks again different character situations */
if(input == ' '){
cin.get(input);
}

if (input=='\n'){
cout << "\n";
cin.get(input);

}

if(input == '/'){
cout << " ";
cin.get(input);
}

morse ="";
}
return 0;
}
Here's an interesting way to translate morse. Just the letters, though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
using namespace std;

bool check(const string& s)
{
    for (char ch: s) if (ch != '.' && ch != '-') return false;
    return true;
}

int main()
{
    const string m{"#ETIANMSURWDKGOHVF*L*PJBXCYZQ**"};

    for (string tok; cin >> tok; )
    {
        if (!check(tok))
            cout << '?';
        else if (tok.size() < 5) // Letter
        {
            int i {1};
            for (char ch: tok) i = i * 2 + (ch == '-');
            cout << m[i - 1];
        }
        else
            cout << '*';
        cout << '\n';
    }
}

Topic archived. No new replies allowed.