Message won't decode, BST.

My message function prints the morse code output rather than the word output. Can you please help me figure this out?

I have deleted a few functions that are not relevant to the question.

I think the error is within the Message function.

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

ofstream out("Morse.out");
ifstream code("MorseCode.in");

struct node
{
	char letter;
    string code;
    node *lchild;
    node *rchild;
};

//Prototypes
void header();
void footer();
void Insert(node*&, string, char);
char Search(node*,string);
void PrintPreOrder(node*);
void PrintInOrder(node*);
void PrintPostOrder(node*);
bool ReadKey(node*&);
bool ReadCode(string&);

//inserting the code into the tree
void Insert(node *&root, string code, char letter)
{
    if(root == nullptr)
    {
		root = new node();
		root->letter = letter;
        root->code = code;
        root->lchild = nullptr;
        root->rchild = nullptr;
    }
    else
    {
        node* child = root;
        node* parent = nullptr;
        node* temp = nullptr;

        while(child)
        {
            parent = child;
            if(letter < child->letter)
                child = child->lchild;
            else
                child = child->rchild;
        }
        temp = new node;
        temp->letter = letter;
        temp->code = code;
        temp->lchild = nullptr;
        temp->rchild = nullptr;

        if(letter < parent->letter)
            parent->lchild = temp;
        else
            parent->rchild = temp;
    }

}

void Message(node* current, string decode)
{
    char ch;
    string message,code;
    int count=0;
    for(int i = 0; i < decode.size(); ++i)
    {
        ch = decode[i];
        if(ch == ' ')
        {
            if(code != "")
            {
                message = message + Search(current,code);
                code = "";
                ++count;
            }
               if(count == 3)  //3 blanks equals a word
               {
                   message = message + " ";
               }
        }
        else if(ch == '.' || ch == '-')
        {
            code=code + ch;
            count = 0;
        }
            else
           {
             out << ch << endl;
           }
    }
}

char Search(node *temp, string str)
{
    if(temp->letter != 0 && str.size() == 0)
    {
        return temp->letter;
    }
    else
    {
        //if there is a dot search lchild else search rchild
        if(str[0] == '.')
        {
            return Search(temp->lchild, str.substr(1));
        }
        else
        {
            return Search(temp->rchild, str.substr(1));
        }
    }
}

bool ReadCode(string& message)
{
	if(!code)
	{
		cout<<"Error opening MorseCode.in";
		return 0;
	}

    char letter;
    while(code.get(letter))
        message += letter;

    code.close();
    return 1;
}

bool ReadKey(node*& root)
{
    ifstream key("MorseKey.in");
	if(!key)
	{
		cout<<"Error opening MorseKey.in";
		return 0;
	}

	string MorseCode;
    char letter;

    while(key >> letter >> MorseCode)
        Insert(root,MorseCode,letter);

    key.close();
    return 1;
}

int main( )
{
	node* root = nullptr;

    string code, message;

    header();
    ReadKey(root);

    //printing the letters in preorder by dots and dashes
    out<<"Letters in preorder"<<endl;
    PrintPreOrder(root);
    out<<endl;

    //printing the letters in order by dots and dashes
    out<<"\nLetters in order"<<endl;
    PrintInOrder(root);
    out<<endl;

    //printing the letters in postorder by dots and dashes
    out<<"\nLetters in postorder"<<endl;
    PrintPostOrder(root);

    ReadCode(message);
    //Message(root,message);

    //printing out the translated message
    footer();
    out << message << endl;


out.close();
return 0;
}
Last edited on
> I have deleted a few functions that are not relevant to the question.
but the calls are still there, generating undefined reference errors if trying to compile
either remove the calls or provide an implementation (even a dummy one) for those functions.


don't have the Morse{Code,Key}.in files.
Topic archived. No new replies allowed.