help with morse code hw

I am supposed to open a .txt file with a phrase written in morse code, write a code to open the file and write a new file with the phrase translated to english. Then write another part to do english back to morse code and I have no idea what to do.

I wrote some crazy code to go one by one through the entire string of morse code to change it to english but obviously I have an abundant amount of errors.

Any advice?


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
  #include<iostream>
#include<fstream>
#include<string>


using namespace std;

int main(){
	std::string text;
	std::string morse;
	
	ifstream morse1("HW4_Morse.txt");
	while(!morse1.eof()){
		getline(morse1, morse);
		cout << morse << endl;
	}
	for(int i=1; i < morse.length();++i)
		if(morse.at(i)=="."){
			i=i+1;
			if(morse.at(i)=="."){
				i=i+1;
				if(morse.at(i)=="."){
					i=i+1;
					if(morse.at(i)=="."){
						text="H";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)=="-"){
						text="V";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)==" "){
						text="S";
						ofstream text("message.txt", ios::append);
					}else{
						text=" ";
						ofstream text("message.txt", ios::append);
				}}else if(morse.at(i)=="-"){
					i=i+1;
					if(morse.at(i)=="."){
						text="F";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)==" "){
						text="U";
						ofstream text("message.txt", ios::append);
					}else{
						text=" ";
						ofstream text("message.txt", ios::append);
					
				}}else if(morse.at(i)==" "){
						text="I";
						ofstream text("message.txt", ios::append);
				}else{
						text=" ";
						ofstream text("message.txt", ios::append);
			}}else if(morse.at(i)=="-"){
				i=i+1;
				if(morse.at(i)=="."){
					i=i+1;
					if(morse.at(i)=="."){
						text="L";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)==" "){
						text="R";
						ofstream text("message.txt", ios::append);
					}else{
						text=" ";
						ofstream text("message.txt", ios::append);
				}}else if(morse.at(i)=="-"){
					i=i+1;
					if(morse.at(i)=="."){
						text="P";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)=="-"){
						text="J";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)==" "){
						text="W";
						ofstream text("message.txt", ios::append);
					}else{
						text=" ";
						ofstream text("message.txt", ios::append);
					
				}}else if(morse.at(i)==" "){
						text="A";
						ofstream text("message.txt", ios::append);
				}else{
						text=" ";
						ofstream text("message.txt", ios::append);
			}}else if(morse.at(i)==" "){
				text="E";
				ofstream text("message.txt", ios::append);
			}else{
				text=" ";
				ofstream text("message.txt", ios::append);
		if(morse.at(i)=="-"){
			i=i+1;
			if(morse.at(i)=="."){
				i=i+1;
				if(morse.at(i)=="."){
					i=i+1;
					if(morse.at(i)=="."){
						text="B";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)=="-"){
						text="X";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)==" "){
						text="D";
						ofstream text("message.txt", ios::append);
					}else{
						text=" ";
						ofstream text("message.txt", ios::append);
				}}else if(morse.at(i)=="-"){
					i=i+1;
					if(morse.at(i)=="."){
						text="C";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)=="-"){
						text="Y";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)==" "){
						text="K";
						ofstream text("message.txt", ios::append);
					}else{
						text=" ";
						ofstream text("message.txt", ios::append);
					
				}}else if(morse.at(i)==" "){
						text="N";
						ofstream text("message.txt", ios::append);
				}else{
						text=" ";
						ofstream text("message.txt", ios::append);
			}}else if(morse.at(i)=="-"){
				i=i+1;
				if(morse.at(i)=="."){
					i=i+1;
					if(morse.at(i)=="."){
						text="Z";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)=="-"){
						text="Q";
						ofstream text("message.txt", ios::append);
					}else if(morse.at(i)==" "){
						text="G";
						ofstream text("message.txt", ios::append);
					}else{
						text=" ";
						ofstream text("message.txt", ios::append);
				if(morse.at(i)=="-"){
					text="O";
					ofstream text("message.txt", ios::append);
				}}else if(morse.at(i)==" "){
					text="M";
					ofstream text("message.txt", ios::append);
				}else{
					text=" ";
					ofstream text("message.txt", ios::append);
			}}else if(morse.at(i)==" "){
				text="T";
				ofstream text("message.txt", ios::append);
			}
		}cout << text << endl;
		
	}morse1.close();
	
		
}
return 0;
}
	
How is the Morse code stored in the file? Each word on a line by itself? Can you print an example of the file?
it uses spaces in between letters and "/" in between words.

- .... . / ... - .- - ..- - . / .-- .- ... / .- / -. --- - .- -... .-.. . / .--. .-. . -.-. ..- .-. ... --- .-. / --- ..-. / - .... . / . ... - .- -... .-.. .. ... .... -- . -. - / -.-. .-.. .- ..- ... . / .- -. -.. / ..-. .-. . . / . -..- . .-. -.-. .. ... . / -.-. .-.. .- ..- ... . / --- ..-. / - .... . / ..-. .. .-. ... - / .- -- . -. -.. -- . -. - / - --- / - .... . / ..- -. .. - . -.. / ... - .- - . ... / -.-. --- -. ... - .. - ..- - .. --- -.


thats the exact text
First, sorry for taking so long to respond. Here is the approach I would take:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Open the file

if that was successful:
    Start a loop to read all of the file's contents:
        Read a line from the file,
        If I detected EOF,
            Break out of the loop
        else
            Start a loop to parse the line:
                Find a "word*"
                If there are no more words,
                    Break out of this loop
                else
                    Store the work in an array
            End of inner loop
        End of outer loop
 


That should get you started -- let me know if have questions.
Last edited on
Topic archived. No new replies allowed.