Exception thrown;read access violation

Exception thrown: read access violation.
node was nullptr. line 55

-program is supposed to translate english to morse code vice versa but the program breaks at line 55 with the exception thrown and im having trouble solving it
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
#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 = NULL;
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 = node->left;
			}
			else if (*morsePtr == '-') {
				node = node->right;
			}
		}
		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 << "encode called" << endl;
	cout << "\sending >>> ";
	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;
	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 << "\enter sentence in english";
	cin.getline(text, 80);
	station.Encode(text, morse);
	cout << morse;
	cout << "message received";
	station.Decode(morse, text);
	cout << "message sent: " << text << endl;	
}
Last edited on
The problem is on line 55 node = node->left;
node is just nullptr;
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
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
using namespace std;

const char Morse[] {"**ETIANMSURWDKGOHVF*L*PJBXCYZQ* "};

char morse_to_char(const string& s) {
    int i = 1;
    for (char ch: s)
        if      (ch == '.') i = i * 2;
        else if (ch == '-') i = i * 2 + 1;
        else break;
    return i < int(sizeof Morse) - 1 ? Morse[i] : '*';
}

string morse_to_string(const string& m) {
    string r, token;
    istringstream in(m);
    while (in >> token) r += morse_to_char(token);
    return r;
}

string to_morse(char ch) {
    const char* p = strchr(Morse, toupper(ch));
    if (!p) return "*";
    string r;
    for (int i = p - Morse; i > 1; i /= 2) r += ".-"[i % 2];
    reverse(r.begin(), r.end());
    return r;
}

string to_morse(const string& s) {
    string r;
    for (char ch: s) { r += to_morse(ch); r += ' '; }
    return r;
}

int main() {
    string line;
    cout << "Enter some words: ";
    getline(cin, line);
    cout << to_morse(line) << '\n';

    cout << "Enter some morse code: ";
    getline(cin, line);
    cout << morse_to_string(line) << '\n';
}

thanks! but unfortunately we have to use a Binary search tree to translate it for an assignment:/
Topic archived. No new replies allowed.