#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    C++ morse code translation, please help


    I have been struggling with this a while now and cant figure out how to do it. So any help would be great.
    This is how it looks at the moment

    Code:
    #include <iostream>
    #include <vector>
    #include <stdio.h>
    #include <string>
    #include <cstring>
    #include <iomanip>
    using namespace std;
    
    
    
    
    
    
    int main ()
    {
      
      char morse[29] [7]={".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.","--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".--.-" , ".-.-", "---."};
      char text [29] [3]={ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "\x86", "\x84", "\x94"};
      int j, k;
      int borja=0, sok, count=0;
      unsigned int i;
      
      while (1)
      {
        
        int find_string = 0;
        int stop_string = 29;
        cout << "What do you want to do?"<<endl
        << "(3) Translate to morse code"<<endl
        << "(2) Translate from morse"<<endl
        << "(1) Exit"<<endl;
        cin >> borja;
        if (borja==3)
        {
          string skrift;
          cout << "--------------------------" <<endl
          << "You have chosen to convert" << endl
          << "From text to morse code," <<endl
          << "What should i translate? : "<<endl;
          cin >> skrift;
          cout << endl << endl << skrift << " is in morse code : ";
          unsigned int str_lenght;
          str_lenght=skrift.size();
          cout << endl;
          
          for (i=0;i<str_lenght;i++)
          {
            find_string=0;
            while (find_string < stop_string)
            {
              if (skrift[i]==text[find_string][0])
              {
                cout << morse[find_string] << " ";
                break;
              }
                else 
                find_string = find_string+1;
            }
          }
        }
        
        else if (borja==2)
        {
            char skrift[50];
          cout << "--------------------------" <<endl
          << "You have chosen to convert" << endl
          << "from morse code to text," <<endl
          << "What should i translate? : "<<endl;
          skrift.getline(skrift, 50, endl) >> skrift;
          cout << endl << endl << skrift << "is in text : ";
          unsigned int str_lenght;
          str_lenght=50;
          cout << endl;
          
          for (i=0;i<str_lenght;i++)
          {
            find_string=0;
            while (find_string < stop_string)
            {
              if (skrift[i]==morse[find_string][0])
              {
                cout << text[find_string];
                break;
              }
              else 
              {
                find_string = find_string+1;
              }
            }
          }
          
        }
        else
          break;
        
        cout << endl << endl << endl << endl;
      }
      cin.get();             
      return 0;
    }
    the first from text to morse i finaly solved it. but the other way around was abit difficult for me. Have only studied programming for a few weeks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    313
    Rep Power
    0
    Morse code is not prefix-free (like Huffman code).

    What is "..." ?

    3 times "e" or one "s" ?

    So you need an additional "separator" symbol, say blank space.

    With this additional symbol use strstr to search and decipher.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by leszek31417
    Morse code is not prefix-free (like Huffman code).

    What is "..." ?

    3 times "e" or one "s" ?

    So you need an additional "separator" symbol, say blank space.

    With this additional symbol use strstr to search and decipher.
    ah yeah that is true. the code will be seperated with blank space. i will try checking about strstr when i finish work today and see if i can figure it out.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480

    Many solutions to Morse code


    http://rosettacode.org/wiki/Morse_code

    Rosetta code

    mmmm---->these are text to Morse.
    Last edited by b49P23TIvg; January 20th, 2012 at 09:21 AM. Reason: oops
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0
    Code:
    #include <iostream>
    #include <vector>
    #include <stdio.h>
    #include <string>
    #include <cstring>
    #include <iomanip>
    using namespace std;
    
    
    
    int main ()
    {
      
      char morse[29] [7]={".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.","--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".--.-" , ".-.-", "---."};
      char text [29] [3]={ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "\x86", "\x84", "\x94"};
      int j, k;
      int borja=0, sok, count=0;
      unsigned int i;
      
      while (1)
      {
        
        int find_string = 0;
        int stop_string = 29;
        cout << "What do you want to do?"<<endl
        << "(3) Convert to Morse code"<<endl
        << "(2) Convert from Morse code"<<endl
        << "(1) Exit"<<endl;
        cin >> borja;
        if (borja==3)
        {
          string skrift;
          cout << "--------------------------" <<endl
          << "You have choseb to convert" << endl
          << "from text to morse code," <<endl
          << "What should i translate?: "<<endl;
          cin >> skrift;
          cout << endl << endl << skrift << " is in morse code : ";
          unsigned int str_lenght;
          str_lenght=skrift.size();
          cout << endl;
          
          for (i=0;i<str_lenght;i++)
          {
            find_string=0;
            while (find_string < stop_string)
            {
              if (skrift[i]==text[find_string][0])
              {
                cout << morse[find_string] << " ";
                break;
              }
                else 
                find_string = find_string+1;
            }
          }
        }
        
        else if (borja==2)
        {
      char skrift[50];
      cout << "--------------------------" <<endl
      << "You have chosen to convert" << endl
      << "from morse code to text," <<endl
      << "What should i translate? Seperate with space: "<<endl;
      std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
      cin.getline(skrift, 50, '\n');
      cout << endl << endl << "what you wrote was " << skrift << endl << "and translates to : " << endl;
      
    //how should i do the seperation?
    
        }
        else
          break;
        
        cout << endl << endl << endl << endl;
      }
      cin.get();			 
      return 0;
    }
    this is how it looks now. But how do i split the string so i can search the other string in similar way as converting from text to morse?
    i will use space as a seperator betwin every "letter"
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0
    have been trying to solve this code for a few weeks now so im feeling abit stupid about it. :chomp:
    Any help would be nice

    Have tried to use string split but it doesnt seem to work so im guessing im using it wrong :chomp:
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    You want a function to split a string? Here's some code I wrote about 7 years ago that does that (you'll need to change the delimiter, of course).

    Here is some more code I wrote that does something similar.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    And yet another version I wrote many moons ago.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Code:
    #if 0
    /*
      Morse to alphabet: compilation and sample run
    
        -*- mode: compilation; default-directory: "/tmp/" -*-
        Compilation started at Wed Jan 25 07:12:05
    
        a=./c && make -k $a && $a ... --- ...  failure -... --- .- done
        cc     c.c   -o c
        sos
        unrecognized
        boa
        unrecognized
    
    
        Compilation finished at Wed Jan 25 07:12:05
    
     */
    #endif
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define STREQ(A,B) (!strcmp((A),(B)))
    #define DIM(A) ((sizeof(A))/(sizeof(*(A))))
    
    char
      *morse[] = {".-","-...",".","...","---"},
      *alpha = "abeso";
    
    void decipher(int c,FILE*ouf) {
      if (c < 0)
        fputs("\nunrecognized\n",ouf);
      else
        fputc(c,ouf);
    }
    
    int translate(char*s) { /* sorting the Morse -> binary search -> faster */
      int i;
      for (i = 0; i < DIM(morse); ++i)
        if (STREQ(s,morse[i]))
          return alpha[i];
      return -1;
    }
    
    int main(int ac,char*av[]) {
      while (NULL != *++av)
        decipher(translate(*av),stdout);
      putchar('\n');
      return EXIT_SUCCESS;
    }
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0
    thanx for all the help :D
    this is how it ended up o.o
    and yes. my skill is less then good :P

    Code:
    #include <iostream>
    #include <vector>
    #include <stdio.h>
    #include <string>
    #include <cstring>
    #include <iomanip>
    using namespace std;
    
    
    
    int main ()
    {
      
      // skapar morse och text för att förvara de olika tecknen för 
      //att senare jämföra och hitta rätt
      char morse[29] [7]={".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.","--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".--.-" , ".-.-", "---."};
      char text [29] [3]={ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "\x86", "\x84", "\x94"};
      int borja=0;
      unsigned int i=0;
      
      while (1)
      {
        int find_string = 0;
        int stop_string = 29;
    
    	int count=0;
    
        cout << "What do you want to do?"<<endl
               << "(5) see the morse code alphabet" << endl
               << "(4) Common errors" << endl
              << "(3) Convert from text to morse code"<<endl
              << "(2) Convert from morse code to text"<<endl
              << "(1) exit"<<endl;
        cin >> borja;
    	cout << endl << endl << endl;
    
    	if (borja==5)
    	{
    		cout << endl << "The morse code alphabet is as follows : " << endl;
    			while (count!=29)
    			{
    				if(i!=3)
    				{
    				  cout << text[count] << " :: " << morse[count] << "  \t" ;
    				  count++;
    				  i++;
    			    }
    				else 
    				{
    				  cout << endl;
    				  i=0;
    				}
    			
    			}
    	}
    
    	else if (borja==4)
    	{
    		cout << "Normal errors" << endl
    			 << "Occurs if you write something that" << endl
    			 << "the program cannot translate" << endl
    			 << "Exampel of this is numbers or other" << endl
    			 << "characters except small letters," << endl
    			 << "\".\" for short code or" << endl
    			 << "\"-\" for long code." << endl
    			 << "The errors will either give" << endl 
    			 << "a beep or end the program "<< endl << endl << endl ;
    	}
    
    
        else if (borja==3)
        {
    
          string skrift;
          cout << "----------------------------------------" <<endl
               << "You have chosen to convert to morse code" << endl
               << "What should i convert? : "<<endl;
          cin >> skrift;
          cout << endl << endl << skrift << " converts to : ";
          unsigned int str_lenght;
    	  str_lenght=skrift.size();
          cout << endl;
          
          for (i=0;i<str_lenght;i++)
          {
    
            find_string=0;
    
            while (find_string < stop_string)
            {
    
    
              if (skrift[i]==text[find_string][0])
              {
    			count=1;
                cout << morse[find_string] << " ";
                break;
    
    			
              }
    
                find_string = find_string+1;
            }
    
          }
    
    	  cout << endl << endl << endl;
    
    	  		if (count==0)
    			cout << endl << "No conversion was done due to an error" << "\a" << endl ;
        }
    
    
        else if (borja==2)
        {
          char skrift[50];
    	  string konvert;
          cout << "------------------------------------------" <<endl
               << "You have chosen to convert from morse code" << endl
               << "What should i convert?" <<endl
    		   << "seperate with space : "<<endl;
    
    	  std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
    
    
          cin.getline(skrift, 50, '\n');
    
    	  cout << endl << endl << "You wrote : " << skrift << endl << "converts to" << endl;
    
    
        char* pskrift = skrift;
    
        char* pend = skrift + strlen(skrift);
    
    
        char* pmorse = morse[0];
    
        while( pskrift < pend )
        {
    
            while( (*pskrift != '.') && (*pskrift != '-') && (*pskrift != 0) )
            {
                pskrift++;
            }
    
    
            char* ptemp = pskrift;
    
    
            while( (*pskrift == '.') || (*pskrift == '-') )
            {
    
                pskrift++;
            }
    
    
            *pskrift++ = '\0';
    
    
            for (i=0;i<29;i++)
            {
    
                if( strcmp( morse[i], ptemp ) == 0 )
                {
                    cout << text[i];
    				count=1;
    
                    break;
                }
            }
        }
    	if (count==0)
    	cout << endl << "No conversion was done due to an error" << "\a" << endl ;
        }
        else
          break;
        
        cout << endl << endl << endl << endl;
      }
      cin.get();			 
      return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo