Thread: Please help me

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    Please help me

    I am trying to decrypt a morse code to regular letters, but i can't get it quite right
    the output i am getting is wrong.
    can some one tell me what i am doing wrong.

    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
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Pease anyone here that can help out ?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    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];

    that's basically what you need to do,.

    don't use temporary variables if you don't need to either, and if you do give them intuitive names because that is just sloppy and all of that overall is just painful to sort through. ^_^

    also, if something does not work then you are *guaranteed to find the error stepping through your code line by line by hand, eventually the mistake in your logic will be made clear to you.
    Last edited by since; 11-11-2009 at 02:00 PM.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    hhh my english isn't that good, and i can agree with sloopy code, but i still dont understand this:
    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];

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    ex if input is :.- .--. . then the output should be APE
    i just can't figure it out

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    someone pls put the correct code for me

  7. #7
    Allways learning cs_student's Avatar
    Join Date
    Aug 2008
    Location
    ~/
    Posts
    39
    A char can easily be casted into an int. You can use this to find the corresponding Morse code without having a separate array.

    In ascii the character 'A' is represented by 65. Thus if the Morse code array is sorted in alphabetical order you just have to cast the character to a int and subtract the correct amount.
    You have to take in account lowercase letters as well since they have different ascii values.

    Consider the following program
    Code:
    #include <iostream>
    
    
    int main(int argc, char** argv)
    {
            std::cout << (int)'A' << std::endl;
            std::cout << (int)'a' << std::endl;
            return 0;
    }
    the output will be
    65
    97

    So to obtain the correct index value you can use something along the lines of
    Code:
            int index;
            if(my_char >= 97) {
                    index = mychar - 97;
            } else {
                    index = mychar - 65;
            }
    Regards,


    cs_student

Popular pages Recent additions subscribe to a feed

Tags for this Thread