Converting an English text to Morse code and vice versa

Hi guys, I have an assignment for my C++ class and the topic is, well the title says it all. I'm having trouble debugging my code, and I was wondering if you guys can help me.
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;
char engtomol (char[], char[], int);
char moltoeng (char[], char[], int);
int main ()
{
    char capalpha[26] = {'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'};
    char morse[26] = {'.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..'};
    char text, morsecode;
    char choice;
    char repeat='y';
    int lengthoftext=text.length();
    while (repeat =='y')
    {
    cout << "Select 1 to encode English text to Morse code. " << "\n" << "Select 2 to decode Morse code to English text." << endl;
    cin >> choice;
    
    if (choice==1)
    {
        cout << "Type in a text that you would like to translate." << endl;
        getline(cin,text);
        cout << "TEXT: " << text << endl;
        cout << "MORSE CODE: " << engtomol (capalpha, morse, lengthoftext) << endl;
        
    }
    
    if (choice==2)
    {
        cout << "Type in a morse code that you would like to translate." << endl;
        cin >> morsecode;
        cout << "MORS CODE: " << morsecode << endl;
        cout << "TEXT: " << moltoode (capalpha, morse, lengthoftext) << endl;
    }
    cout << "Would you like to try again? Hit y to continue." << endl;
    cin >> repeat;
    }    
return 0;
}

char engtomol (char capalpha[], char morse[], int lengthoftext)
{
    for (int k=0; k<lengthoftext; k++)
    {
        capalpha[k]=morse[k];
        return morse[k];
    }
}

char moltoeng (char capalpha[], char morse[], int lengthoftext)
{
    for (int k=0; k<lengthoftext; k++)
    {
        morse[k]=capalpha[k];
        return  capalpha[k];
    }
}


the errors and/or warnings my compiler says is that, for char morse[26], it all shows a multi character constant warning, no matching function for call to 'get line' and member reference base type 'char' is not a structure or union.
I'm pretty sure my code right now doesn't do the job but I just want to debug it at the moment.

Thank you.
how many chars do you reckon this is:
'.-'?
You're trying to squeeze too much into a character type :)

I think you'll need your morse array to be strings.

or consider a std::map<char, std::string>
Last edited on
hey mutexe,
I edited my code, and the thing is that I am trying to put a space between each morse code and 3 spaces between each word (ex: sos sos = ... --- ... ... --- ...) but the thing is, it's not showing up right. I succeeded in having a single space between the letters, but not between the words. I'll post my edited code below.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string engtomol (string, string[]);
int main ()
{
    char alpha[26] = {'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'}; 
    string morse[81] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
    string text, morsecode;
    char choice;
    char repeat='y';
    int lengthoftext=text.length();

    {
        cout << "Enter Morse code to translate, with a space between letters and three spaces between words:" << endl;
        getline(cin,text);
        cout << "TEXT:" << text << endl;
        cout << "MORSE CODE: " << engtomol(text, morse) << endl;
        
    }
    
return 0;
}    



string engtomol (string text, string morse[])
{
    int textlength = text.length();
    string spacesbtwletters= " ";
    string spacesbtwwords = "   ";
    for (int k=0; k<textlength; k++)
    {
if (text[k]!= ' ') //in case single words are inputted
{   text[k]=toupper(text[k]); //upper case letters and lowe case letters have the same appropriate morse code.
    spacesbtwletters += morse[text[k]-'A']; 
    spacesbtwletters+=" "; 
}
if (text[k]==' ')
{
    spacesbtwwords+=morse[text[k]-'A'];
    spacesbtwwords+= "   ";
}
} 
return spacesbtwletters; 
}
Topic archived. No new replies allowed.