I have a function in my program that is supposed to take Morse code input, compare it to an array of strings and return a letter from a corresponding string once it has found the matching Morse. I've finally managed to get it to run without crashing, but now it keeps returning the wrong letters. For instance ... --- ... should return sos but instead I get amb. I tried testing it by printing out the index number, the morse code string and the letter and it all matched up, so I think the problem is with string compare.
Here's the code:
void morsetotext(char mor[])
{
char alpha[]={"abcdefghijklmnopqrstuvwxyz1234567890 "};
char *morse[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
"..", ".---","-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-",".--", "-..-", "-.--", "--.","-----", ".----",
"..---", "...--", "....-",".....", "-....", "--...", "---..", "----." "/ "};
char letter[8];
char convert[250];
int con_count=0;
int let_count=0;
int count=0;
int index=0;
int length=strlen(mor);
while (count<length)
{
for(let_count=0; let_count<8 && mor[count]!=' '; let_count++)
{
letter[let_count]=mor[count];
count++;
}
letter[let_count+1]='\0';
index=0;
while (strcmp (letter, morse[index])!=1)
{
index++;
}
count++;
printf ("%c", alpha[index]);
}
return;
}
Thanks for any help.
Edit: Sorry about that, here's the whole function.
letter. Sounds like it may be achar, in which case your call tostrcmpis wrong. Do you have your warning level turned up? Are you ignoring any warnings?