Encoding/decoding morse code

This is a discussion on Encoding/decoding morse code within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a program that encodes alphabetical characters from a file into morse code, and vise versa. I ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    16

    Encoding/decoding morse code

    I'm trying to write a program that encodes alphabetical characters from a file into morse code, and vise versa. I have it partially written, but I've confused myself while trying to write the encode and decode functions:

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Code
    {
    public:
    
      Code();  // Default constructor - loads and uses morse code
    
      Code(vector<int> codewords);  // constructor loading customized code
    
      string encode(vector<char> message);  // encodes a message consisting of A-Z
    
      string decode(vector<string> message);  // decodes a message
    
    private:
    
      vector<string> codewords; // this is a codeword vector parallel to A-Z
      vector<char> alpha; // this is the vector A-Z
    
      vector<char>  alphacode(); // function returns vector - A B C etc.
    
      vector<string>  morsecode(); // function returns vector containing morse code
    
      string encode(char x); //returns the codeword for the character x
      string decode(string c); //returns the character for the codeword c.
    
    
    };
    
    Code::Code(){
    	this->codewords=morsecode();
    	this->alpha=alphacode();
    }
    
    Code::Code(vector<int> codewords)
    {
    }
    
    string Code::decode(vector<string> message)
    {
    	int w=0;
    	vector<char> alph=alphacode();
    	string out;
    	while(w<message.size()){
    		string temp = ?
    		for(int i=0;i<alph.size();i++)
    		{
    			if(temp == codewords[i])
    			{
    				out += alph[i];
    			}
    			w+=temp.size();
    		}
    	}
    	return out;
    }
    
    string Code::encode(vector<char> message)
    {
    	//message = morsecode();
    	//string morse;
    }
    
    vector<char> alphacode()
    {// This returns a vector containing the alphabet a-z and " "
    	vector<char> temp;
    	for (char c='A'; c<='Z'; c++)
    	temp.push_back(c);
    	temp.push_back(' ');
    	temp.push_back('.');
    	return temp;
    }
    
    vector<string> morsecode()
    { // This function returns a vector containing the morse code vector<string> temp(28);
     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[26] ="x";
     return temp;
    }
    
    
    
    int main()
    {
    	Code t;
    	vector<string> mess;
    	char x;
    	cin >> x;
    	while(cin.good())
    	{
    		mess.push_back(x);
    		cin>>x;
    	}
    	cout << t.decode(mess) << endl;
    	Code m;
    	string HW = "Hello";
    	cout << m.encode(HW) << endl;
    }
    Any help is much appreciated!
    Last edited by bigboybz; 02-16-2011 at 11:35 AM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    I think your last temp[26] is meant to be temp[27].
    Please tell us a bit more about how you're confused, and perhaps if you'd fix the indentation it would be easier for all to read, ta.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    All you need is a struct that will hold the encoded and decoded versions of a letter. Then you'll iterate through them all and if you find the former/latter you'll know that it's the latter/former! Simple!
    Devoted my life to programming...

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Look into using an editor/IDE that actually indents your code. The layout is horrible. I'm gonna guess you're using Dev-C++.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    I hope you are going to put spaces between each coded symbol or it will be impossible to decode it. Morse isn't prefix-free, so unless you know the boundaries between symbols the decoding is ambiguous.

    For instance, "..." could be "eee" or "s". The way people tell the difference when actually using Morse code is to put a little pause between letters.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by rags_to_riches View Post
    Look into using an editor/IDE that actually indents your code. The layout is horrible. I'm gonna guess you're using Dev-C++.
    I say he's using a Text Editor!

    ( Dev-C++ indents your code all-right, by the way! )
    Devoted my life to programming...

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Sipher View Post
    I say he's using a Text Editor!
    Which is better than editing code in Word, which I've seen done before.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by brewbuck View Post
    Which is better than editing code in Word, which I've seen done before.
    You mean MS Word, right?

    Yeah, that would be hilarious. His compiler would go nuts!!
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    Sorry about that, I fixed the indenting.

    What would I set temp to under the decode function?

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Sipher View Post
    You mean MS Word, right?

    Yeah, that would be hilarious. His compiler would go nuts!!
    What the person would do is, load .c file into Word as a text file, write his code (putting "important stuff" in bold or italics or even a different point size), then save it as text. Then he'd be baffled when the next time he opened it all his careful formatting was wiped out.

    I'm amazed he figured out the "save as text" part. I can only assume some similarly misguided person taught him how to do it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    I'm sad, my thread was stolen by ms word talk =(

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    The part i'm confused on is how can I get the characters from a file into a vector<char>?

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    As i said you need something like this:
    Code:
    #include <string>
    #include <vector>
    
    using namespace std;
    
    struct Morse
    {
        char Letter;
        string Code;
    };
    
    typedef vector<Morse> MorseCode;
    The rest ... feagure it out yourself!
    Devoted my life to programming...

  14. #14
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    Or a map:
    Code:
    std::map<char, std::string> morse;
    morse['a'] = ".-"
    //...
    Encoding would be simple, decoding would be too, but you'd have to either search the code by value (equal_range - C++ Reference) to get your original char out of the map, or make yourself second map<string, char>, and use it just like the original.

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    OK, I'm using an iterator to point to each character that the file has, and this is the code i've come up with (encoding atleast)
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    struct Morse
    {
        char Letter;
        string Code;
    };
    
    typedef vector<Morse> MorseCode;
    
    class Code
    {
    public:
    
      Code();  // Default constructor - loads and uses morse code
    
      Code(vector<int> codewords);  // constructor loading customized code
    
      string encode(vector<char> message);  // encodes a message consisting of A-Z
    
      string decode(vector<string> message);  // decodes a message
    
    private:
    
      vector<string> codewords; // this is a codeword vector parallel to A-Z
      vector<char> alpha; // this is the vector A-Z
    
      vector<char>  alphacode(); // function returns vector - A B C etc.
    
      vector<string>  morsecode(); // function returns vector containing morse code
    
      string encode(char x); //returns the codeword for the character x
      string decode(string c); //returns the character for the codeword c.
    
    
    };
    
    Code::Code(){
    	this->codewords=morsecode();
    	this->alpha=alphacode();
    }
    
    Code::Code(vector<int> codewords)
    {
    }
    
    
    string Code::encode(vector<char> message)
    {
    	int e=0;
    	vector<string> morse = morsecode();
    	vector<string>v;
    	vector<string>::iterator it;
    	string output;
    	while(e<message.size()){
    		for(it=v.begin();it!=v.end();it++)
    		{
    			//return it;
    			e=v.size();
    		}
    		
    	}
    	return output;
    }
    
    vector<char> alphacode()
    {// This returns a vector containing the alphabet a-z and " "
    	vector<char> temp;
    	for (char c='A'; c<='Z'; c++)
    	temp.push_back(c);
    	temp.push_back(' ');
    	temp.push_back('.');
    	return temp;
    }
    
    vector<string> morsecode()
    { // This function returns a vector containing the morse code 
    vector<string> temp(28);
     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[26] ="x";
     return temp;
    }
    
    
    
    int main()
    {
    	Code t;
    	vector<string> mess;
    	char x;
    	cin >> x;
    	while(cin.good())
    	{
    		mess.push_back(x);
    		cin>>x;
    	}
    	cout << t.decode(mess) << endl;
    	Code m;
    	string HW = "Hello";
    	cout << m.encode(HW) << endl;
    }
    I would love to compile and test my work to fix everything that I know is wrong with this, but I keep getting a little error "invalid conversion from ‘char’ to ‘const char*’" in line 142 (under my int main). Can someone help me understand what this means, and what i'm doing wrong?
    Last edited by bigboybz; 02-17-2011 at 12:55 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do I morse code converting program
    By panfilero in forum C Programming
    Replies: 17
    Last Post: 10-29-2005, 09:16 PM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. About the Morse code Converter
    By Amber_liam in forum C Programming
    Replies: 17
    Last Post: 05-29-2002, 08:35 AM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21