Need help on morse code program!

Hi,
I have been assigned to make a program that decodes a message in morse code to english and that's what I have done... Except when I compile the program and feed a message into it, all I get are the same characters and not the correct ones.

For example: I feed the .txt file containing the following message.

-... . ... ..- .-. . - --- -.. .-. .. -. -.- -.-- --- ..- .-. --- ...- .- .-.. - .. -. . x

But what my program output is 'AAAAAAAAAAAAAAAAAAAAAAAAAA'. This is the correct AMOUNT of characters, but again they are not the correct characters.

But here is the code I was able to compile, tell me if you find out what I have done wrong. I am stumped!

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <string>
#include <vector>
#include <iostream>
using namespace std;

class Code
{
public:
    Code();               // Default constructor - loads and uses morse code
    string decode(vector<string> message);          // decodes a message
    
private:
    vector<string> codewords;             // this is a codeword vector parallel to A-Z
    vector<char> alpha;                   // this is the vector for A-Z
    vector<char>  alphacode();            // function returns vector - A B C etc
    vector<string>  morsecode();          // function returns vector containing morse code
    char decode(string c);              //returns the character for the codeword c
};

Code::Code()
{
    alpha=alphacode();  //sets alpha into alphacode
    codewords=morsecode(); //sets codewords into morsecode
}

string Code::decode(vector<string> message)
{
    string temp;
    for (int i=0; i<message.size(); i++)
    {
           temp += decode(message[i]);
    }
    return temp;
}

vector<char> Code::alphacode()
{  // This returns a vector containing the alphabet A-Z and " " and "."
      vector<char> temp;
    for (char c='A'; c<='Z'; c++)		//returns the letters
    {    
          temp.push_back(c);
          temp.push_back(' ');				//returns the space space
          temp.push_back('.');				//returns the period.
     }
    return temp;						
}

vector<string> Code::morsecode()
{                  // This function returns a vector containing the morse code
    vector<string> temp(28);				//compares c
    temp[0] =" .-";
    temp[1] =" -...";
    temp[2] =" -.-.";
    temp[3] =" -..";
    temp[4] =" .";
    temp[5] =" ..-.";
    temp[6] =" --.";
    temp[7] =" ....";
    temp[8] =" ..";
    temp[9] =" .---";
    temp[10] =" -.-";
    temp[11] =" .-..";
    temp[12] =" --";
    temp[13] =" -.";
    temp[14] =" ---";
    temp[15] =" .--.";
    temp[16] =" --.--";
    temp[17] =" .-.";
    temp[18] =" ...";
    temp[19] =" -";
    temp[20] =" ..-";
    temp[21] =" ...-";
    temp[22] =" .--";
    temp[23] =" -..-";
    temp[24] =" -.--";
    temp[25] =" --..";
    temp[26] =" .......";
    temp[27] =" x";
    return temp;
}

char Code::decode(string c)
    {
        for (int i=0;i<alpha.size();i++)
        {
            if(c == codewords[i])
            {
                return alpha[i];
               
            }
            
        }
    }


int main()
{
    vector<string> message;
    string temp;
    Code c;
    
    cin >> temp;
    
    while (cin.good())
    {
            message.push_back(temp);
            cin >> temp;
    }
    cout << c.decode(message) << endl;
    
}
Last edited on
You have a semi-colon you don't want on line 86.
Last edited on
Oh wow, you're completely right, and I overlooked that. But now I've run into a new problem... Instead of the program outputting just A's, it returns nothing?!
Line 51 - 78. All of those strings have a leading space. Remove it.

1
2
3
4
5
6
7
8
9
10
11
12
char Code::decode(string c)
    {
        for (int i=0;i<alpha.size();i++)
        {
            if(c == codewords[i])
            {
                return alpha[i];
               
            }
            
        }
    }


If a string isn't found, what is returned?
Ahhh! Thank you! You're a life saver! Such simple mistakes and I overlooked them both. XD

Works perfectly now! :)
Alright so I ran into a bit of confusion again. I made a program that encodes from alphabetic to morse, and a program that decodes from morse to alpha. Now I am required to combine the two into one program. So, I am having trouble successfully combining the two int mains and was wondering if anyone could give me suggestions on how to approach this.

Here's the code with all the encode and decode class functions, and both int mains for encode and decode as noted by the comments.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Code
{
public:
    Code();                                       // Default constructor - loads and uses morse code
    string decode(vector<string> message);          // decodes a message
    string encode(vector<char> message);          // encodes a message
    
private:
    vector<string> codewords;             // this is a codeword vector parallel to A-Z
    vector<char> alpha;                   // this is the vector for A-Z
    vector<char>  alphacode();            // function returns vector - A B C etc
    vector<string>  morsecode();          // function returns vector containing morse code
    char decode(string c);              //returns the character for the codeword c
    string encode(char c);              //returns the string for the codeword c
};

Code::Code()
{
    alpha=alphacode();
    codewords=morsecode();
}

string Code::decode(vector<string> message)
{
    string temp;
    for (int i=0; i<message.size(); i++)
    {
        temp += decode(message[i]);
    }
    return temp;
}

string Code::encode(vector<char> message)
{
    string temp;
    for (int i=0; i<message.size(); i++)
    {
        temp+=encode(message[i]);
    }
    return temp;
}

char Code::decode(string c)
{
    for (int i=0;i<alpha.size();i++)
    {
        if(c == codewords[i]) //looks for codeword[i] and matches it with alpha [i]
        {
            return alpha[i]; //returns c as a char in alpha
            
        }
        
    }
}


string Code::encode(char c)
{
    for (int i=0;i<codewords.size();i++)
    {
        if (c==alpha[i]) 				
        {
            return codewords[i];					
        }
    }
}

vector<char> Code::alphacode()
{                                          // This returns a vector containing the alphabet A-Z and " " and "."
    vector<char> temp;
    for (char c='A'; c<='Z'; c++)		//returns the letters
        temp.push_back(c);
    temp.push_back(' ');				//returns the space space
    temp.push_back('.');				//returns the period.
    return temp;						
}

vector<string> Code::morsecode()
{                                         // This function returns a vector containing the morse code
    vector<string> temp(28);				//compares c
    temp[0] =".-";
    temp[1] ="-...";
    temp[2] ="-.-.";
    temp[3] ="-..";
    temp[4] =".";
    temp[5] ="..-.";
    temp[6] ="--.";
    temp[7] ="....";
    temp[8] ="..";
    temp[9] =".---";
    temp[10] ="-.-";
    temp[11] =".-..";
    temp[12] ="--";
    temp[13] ="-.";
    temp[14] ="---";
    temp[15] =".--.";
    temp[16] ="--.--";
    temp[17] =".-.";
    temp[18] ="...";
    temp[19] ="-";
    temp[20] ="..-";
    temp[21] ="...-";
    temp[22] =".--";
    temp[23] ="-..-";
    temp[24] ="-.--";
    temp[25] ="--..";
    temp[26] =".......";
    temp[27] ="x";
    return temp;
}

int main()
{
    
    //this is the int main code for decode
    vector<string> message;
    string temp;
    Code c;
    
    cin >> temp;
    
    while (cin.good())
    {
        message.push_back(temp);
        cin >> temp;
    }
    
    cout << c.decode(message) << endl;
    
    
    
    //this is the int main code for encode
    vector<char> message;	 
    string temp; 
    
    getline(cin, temp);	 
        for (int i=0; i <temp.length(); i++)
        {
            message.push_back(temp[i]);
        }
    
    Code C;
    cout << C.encode(message) << endl;
    
   
}
Last edited on
I would use a 2d array of structs, with letters A-Z (a char) as one member, and the morse equivalents (a string) as the other member. Also a string variable to store the input morse word in, and a char variable to store the input letter.

I don't see the need for vectors, because the data is not going grow.

This makes encoding and decoding easy, because it is just a lookup based on the char or input morse word, respectively. This can be achieved with a simple for loop.

HTH
Yeah... unfortunately the assignment for the class requires the use of vectors, so I can't do that. :/
This makes encoding and decoding easy, because it is just a lookup based on the char or input morse word, respectively. This can be achieved with a simple for loop.


Except this how encoding and decoding is done now. Achieved with a simple for loop.

Just ask the user if they're entering something to be converted to morse code or converted from morse code, then use the appropriate code.
Yeah I figured it all out, guess I needed to sleep on it! Thanks guys again! :)
Topic archived. No new replies allowed.