|
-
November 11th, 2009, 02:21 PM
#1
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;
}
-
November 11th, 2009, 03:10 PM
#2
-
November 11th, 2009, 03:12 PM
#3
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.
-
November 11th, 2009, 03:24 PM
#4
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?
-
November 11th, 2009, 03:25 PM
#5
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.
-
November 11th, 2009, 03:31 PM
#6
Re: Again pls help
ex if input is :.- .--. . then the output should be APE
i just can't figure it out
-
November 11th, 2009, 03:37 PM
#7
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.
-
November 11th, 2009, 03:42 PM
#8
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]
-
November 11th, 2009, 03:52 PM
#9
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.
-
November 11th, 2009, 04:00 PM
#10
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.-
-
November 11th, 2009, 04:03 PM
#11
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];
-
November 11th, 2009, 04:05 PM
#12
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 ...___?
-
November 11th, 2009, 04:09 PM
#13
Re: Again pls help
Yes i am putting a space between them
-
November 11th, 2009, 04:18 PM
#14
Re: Again pls help
 Originally Posted by cijagm
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|