help with morse decode program

hi im writing a program to decode a morse code message. my program compiles without any error but nothing happens when i enter my code to be decoded. i think there might be something wrong in the "int main()" part of my program. i could really use some help.

im trying to decode this message:
-.. --- -. - ....... ..-. --- .-. --. . - ....... - --- ....... -.. .-. .. -. -.- ....... -.-- --- ..- .-. ....... --- ...- .- .-.. - .. -. . x

this is my code:

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

using namespace std;

class Code 
{public:
Code();
string decode(vector<string> message);
private: 
vector<string> codewords; // codeword vector parallel to A-Z 
vector<char> alpha; // 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); 
};

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; 
} 

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 
} 
}
}

vector<char> Code::alphacode() 
{vector<char> temp; // returns a vector containing the alphabet A-Z and " " and "." 
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() 
{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() 
{vector<string> message; //this is the int main code for decode 
string temp; 
Code c; 
cin >> temp; 
while (cin.good()) 
{message.push_back(temp);
cin >> temp; 
} 
cout << c.decode(message) << endl;
}
Last edited on
Please repost your code using the code tags -- you'll find them in the Format: section to the right of the pages.
i edited to use code tags, thanks koothkeeper
You really need to format your code to be more readable. You will have to change your login in main(). I put the string of morse code into a file and ran your program like this:

1
2
3

cat input | main


What happens is that all of the contents of the input is assigned to temp -- not each morse character. You will want to think about parsing the result of cin >> temp, then pushing back.

And by the way: I HATE OVALTINE! ;) Just kidding!
I'm not sure what you mean ? So I have to put the morse code I want to be decoded in a file then run my program. Then pass the code before pushing back onto vector.

What is cat input l main? Does this go in "int main()"?
The command:
1
2
3

cat input | main


is a way to run your program on UNIX, Linux, and even Windows. The "cat" command opens the file named "input" and writes its contents on the standard output device (usually your monitor). The word "main" is the name of your program -- I simply chose that name.

You don't have to put your code into a file: It's just a very quick way to test, over and over.

What I was trying to say is that when you use:

1
2
3

cin >> temp;


All of the characters that you type get stored into temp, but temp doesn't know when you are finished with the string. At the end of the string you can type ctrl-d (which means end-of-file) and the program will work.

It would be better to use:

1
2
3

getline( cin, temp );


That should help a lot!
Topic archived. No new replies allowed.