Morse code translator exception thrown line 66

- the program translates english to morse fine. But when translating morse to english the code breaks at line 66 and tells me exception thrown and wont translate it. Ive been stuck on this awhile any help is appreciated!
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
#pragma comment(lib,"d3d9.lib")
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <string>
#include <cctype>
#include<stdlib.h>

using namespace std;
struct TREENODE {//constructor
	char letter;//to be replaced by a letter
	TREENODE* left;//declare pointer
	TREENODE* right;//declare pointer
	TREENODE() {
		letter = ' ';
		left = 0;
		right = 0;
	}
};
struct MORSECODE {//each morse code object has
	char letter;//english letters
	char code[100];//dots and dashed
};
class TELEGRAPH {//binary tree
private:
	static MORSECODE table[100];
	static TREENODE* root;
	static void destroyTree(TREENODE *NODE);
public:
	TELEGRAPH() {
		root = NULL;
	}
	static void buildTree();

	void Encode(char text[], char morse[]);
	void Decode(char morse[], char text[]);
};
MORSECODE TELEGRAPH::table[100] = {
   {'E', ". " }, { 'T', "- " }, { 'I', ".." }, { 'A', ".- " }, { 'N', "-. " }, { 'M', "-- " },
   {'S', "... "}, {'U', "..- "}, {'R', ".-. "}, {'W', ".-- "}, {'D', "-.. "}, {'K', "-.- "},
   {'G', "--. "}, {'O', "--- "}, {'H', ".... "}, {'V', "...- "}, {'F', "..-. "}, {'L', ".-.. "},
   {'P', ".--. "}, {'J', ".--- "}, {'B', "-... "}, {'X', "-..- "},
   {'C', "-.-. "}, {'Y', "-.-- "}, {'Z', "--.. "}, {'Q', "--.- "}, {' ', "---- "}
};
TREENODE* TELEGRAPH::root = 0;
void TELEGRAPH::Decode(char morse[], char text[]) {
	char* morsePtr;//points to the dots and dashes in the table
	TREENODE* node;//declare pointer
	node = root;
	cout << "Decode called" << endl;

for (morsePtr = morse; *morsePtr; morsePtr++) {//goes through the morse code that letter/symbol
	if (*morsePtr != ' ') {
			if (*morsePtr == '.') {
				node = nullptr;
			}
			else if (*morsePtr == '-') {
				node = nullptr;
			}
		}		else {
			node = root;
		}
		continue;
	}
	*text++ = node->letter;
	return;
}
void TELEGRAPH::Encode(char text[], char morse[]) {
	int i;
	char c, * t, * morsePtr;//goes through the morse code for that letter/symbol 

	cout << "Your message in morse code is: ";
	for (t = text; *t; t++) {
		c = toupper(*t);
		if (c == ' ') {
			*morse++ =' ';
			continue;
		}
		for (i = 0; table[i].letter; i++) {
			if (table[i].letter == c)break;
		}
		if (!table[i].letter) {
			continue;
		}
		morsePtr = table[i].code;
		while (*morsePtr) {
			*morse++ = *morsePtr++;
		}
		*morse++ = ' ';
	}
};

void TELEGRAPH::buildTree() {
	TREENODE* node, * nextNode;
	char* morsePtr;
	char* textPtr;
	root = new TREENODE;
	if (!root) {
		return;
	}
	root->letter = ' ';
	cout << "alphabet in morse";
	for (int i = 0; table[i].letter; i++) {
		node = root;
		for (morsePtr = table[i].code; *morsePtr; morsePtr++) {
			if (*morsePtr == ' ') {
				cout << *morsePtr;
				nextNode = new TREENODE;
				node->right = nextNode;
				node = node->right;
			}
			else if (*morsePtr == '.') {
				cout << *morsePtr;
				nextNode = new TREENODE;
				node->left = nextNode;
				node = node->left;
			}
		}
	}
};
int main() {
	TELEGRAPH station;
	char text[100], morse[600];
	station.buildTree();
	cout << "\nenter sentence in english: ";
	cin.getline(text, 100);
	station.Encode(text, morse);
	cout << morse;
	cout << "\nenter a sentence in morse code with a space in between each letter";
	cin.getline(morse, 100);
	station.Decode(morse, text);
	cout << text;
	cout << "message sent: " << text << endl;	

}
Last edited on
remove the "n" from the closing code tag.
[/code] not [/ncode]
thanks should be formatted now
Last edited on
Your indentation is misleading. It's important to have consistent indentation when showing your code to others.
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
void TELEGRAPH::Decode(char morse[], char text[]) {
    
	char* morsePtr;
	TREENODE* node;
	node = root;
	cout << "Decode called" << endl;

	for (morsePtr = morse; *morsePtr; morsePtr++) {
		if (*morsePtr != ' ') {
			if (*morsePtr == '.') {
				node = nullptr;
			}
			else if (*morsePtr == '-') {
				node = nullptr;
			}
		}
		else {
			node = root;
		}
		continue;
	}
    
	*text++ = node->letter;
	return;
}

What happens when the last letter of the c-sting that morsePtr points to is '.' or '-'?
node gets set to nullptr.
What happens when you dereference a null pointer? Undefined behavior (usually a crash, if you're lucky).

PS: The continue at the end of a for loop doesn't do anything.
Last edited on
it was originally:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for (morsePtr = morse; *morsePtr; morsePtr++) {
		if (*morsePtr != ' ') {
			if (*morsePtr == '.') {
				node = ->right;
			}
			else if (*morsePtr == '-') {
				node = ->left;
			}
		}
		else {
			node = root;
		}
		continue;
	}
    
	*text++ = node->letter;
	return;
}

but i would receive the same exception thrown issue on lines 4 and 7. so what would be the alternative my code above^^?
Last edited on
Could you please stop creating new threads every time you run into a new issue? Just ask any follow-up questions you have in this thread.
Ah, didn't know this was related to a previous thread.

Well, since I'm already here..
nolecelson, I assume you mean, node = node->right.
But you'll get the same issue if node eventually gets set to a nullptr!

First: Notice that you only ever hit line 66 (my line 23) one time. You're not modifying each letter, you'd only ever modify the first letter (that is, you theoretically would be, if it weren't crashing).

If *morsePtr == ' ', that means you've entered a new word, and you should update text here.

But the issue is must be some issue with how you're building your tree, because you're traversing your tree, but then eventually you're hitting a leaf of the tree too soon.

for (int i = 0; table[i].letter; i++) {
This is the first questionable thing I see. When is table[i].letter ever a null character?

Second, what happens if *morsePtr == '-' in your buildTree() function? That's probably the biggest error here.
Last edited on
ah thanks Ganado, that fixed my issue!
Topic archived. No new replies allowed.