CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Again pls help

  1. #1
    Join Date
    Nov 2009
    Posts
    22

    Again pls help

    I am trying to decrypt a morsecode to letter's, the input should be morsecode and output should be letter's i have been trying for the last 4-5 hour's but i can' get it right, i know something is wrong with the 2nd for loop since is only looping trough the first character in the morse code , but how do i change that.

    here is the code:

    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <vector>
    using namespace std;
    
    
    string morse(string a);
    int main()
    {
    
    string ord;
    
    cout<< "\nwrite your morsecode  "	<<endl;
    cin>>ord;
    
    
    cout<<"the morse code decrypted is "<<morse(ord);
    
    system("PAUSE");
    return 0;
    }
    
    
    string morse(string a)
    {
    char g;
          string z[30]={".- ","-... ","-.-. ","-.. ",". ","..-. ","--. ",".... ",".. ",".--- "
                  ,"-.- ",".-.. ","-- ","-. ","--- ",".--. ","--.- ",".-. ","... ","- "
                  ,"..- ","...- ",".-- ","-..- ","--.. ",".--.- ",".-.- ","---. "};
          string u[30]={"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","Å","Ä","Ö"}; 
          string klart="";                  
         
          for (int i = 0;i <a.size();i++)
          {
          char f =a [i];
          for (int j = 0; j <=29;j++)
          {
           string tmp=z[j];
             g=tmp[0];
            if(g==f)
          { 
            klart=klart +u[j];  
          
          }
           
          }  
          }    
    return klart;
    }

  2. #2
    Join Date
    Nov 2009
    Posts
    22

    Re: Again pls help

    pls someone!!

  3. #3
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Again pls help

    I think you need to change the logic and use string::compare method to search for the string in the string array rather than search character by character.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  4. #4
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Again pls help

    1. Show some consideration to those from whom you are asking for help & properly format that mess.

    2. What happened when you debugged it?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Again pls help

    I can't tell from your logic what you're trying to do. Basically you need to break the input string into its component more code characters, ... ._, etc. and look at each element in your array z for a match, then output the element in the some location of array u.

  6. #6
    Join Date
    Nov 2009
    Posts
    22

    Re: Again pls help

    ex if input is :.- .--. . then the output should be APE
    i just can't figure it out

  7. #7
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Again pls help

    What is the delimiter for the codes?
    Is it a space?

    If so you can use the find and substr methods to extract each code from the input string and then use compare to search in the array.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  8. #8
    Join Date
    Nov 2009
    Posts
    22

    Re: Again pls help

    i don't know what the word delimiter mean but you should separet the the code by space
    hmm ok so substr from input and then compare it to u[30]

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Again pls help

    It looks like the strings in your z array include spaces at the end of each of them. This is most likely going to make your life more difficult; I suggest removing those spaces.

  10. #10
    Join Date
    Nov 2009
    Posts
    22

    Re: Again pls help

    yeah maybe but that's not the main problem, the problem is:
    Code:
    for (int j = 0; j <=29;j++)
     {
       string tmp=z[j];
        g=tmp[0];
         if(g==f)
    }
    this will only look for the first character in string z[30] which is . not the whole 1stelement which is.-

  11. #11
    Join Date
    Nov 2009
    Posts
    22

    Re: Again pls help

    what does this mean:
    input : string sA;
    the return string sR="";
    for each char cA in string sA;
    find the index of cA in u[] = cI;
    save the associated value of z[] at index cI, sR += z[cI];

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Again pls help

    The first thing you need to do is figure out how to break up your input into separate Morse letters. Are you putting a space between them as in ... ___ or just stringing them all together, like ...___?

  13. #13
    Join Date
    Nov 2009
    Posts
    22

    Re: Again pls help

    Yes i am putting a space between them

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Again pls help

    Quote Originally Posted by cijagm View Post
    Yes i am putting a space between them
    Okay, then I already told you what to do. You need to separate the input Morse characters and find them in your array of Morse characters, then output the corresponding letter from your other array.

    Throw out your loop code and try implementing what I just said. Using meaningful variable names will help you and others keep track of what you're doing. Using single character variable names is a horrible way to code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured