help with morse decode program
Nov 16, 2015 at 5:22am UTC
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 Nov 16, 2015 at 5:22am UTC
Nov 16, 2015 at 5:28am UTC
my program compiles without any error but nothing happens when i enter my code to be decoded.
Are you doing something that will cause the condition on line 88 to evaluate to false?
Nov 16, 2015 at 6:06am UTC
i want to input a morse code and output a decoded morse code.
that is what im looking to do but cant seem to figure out.
nothing should come back false in line 88, so do i not need that part?
Nov 16, 2015 at 6:13am UTC
If the condition on 88 never evaluates to false, the loop will never exit.
Nov 16, 2015 at 6:27am UTC
ok so my int main should look like:
1 2 3 4 5 6 7
int main()
{vector<string> message; //this is the int main code for decode
string temp;
Code c;
cin >> temp;
cout << c.decode(message) << endl;
}
Topic archived. No new replies allowed.