morse code

i have no idea y the output are all "A" and "B"...help me plz..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    string morse[39]={".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-",".-..", "--",
        "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
        ".----", "..---", "...--", "...._", ".....", "-....", "--...", "---..", "----.", "-----",
        ".-.-.-", "--.--", "..--.."};
    string plaintext[39]={"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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 
        "Stop", ",", "?"};
    string morsecode;
    int textlength;
    int count=0, i=0;
    cout<<"1-3 Enter a string to convert to Morse Code.";
    cout<<"Convert Morse Code to Text";
	cout<<"Enter Morse Code(sperate by space): ";
	getline(cin, morsecode);
	for(int i=0; i<morsecode.size(); i++)
	{
		int find_string=0;
		while (find_string < 39)
		{
			if (morsecode[i]==morse[find_string][0])
			{
				count=1;
				cout << plaintext[find_string] <<" ";
				break;
			}
			find_string+=1;
		}
	}
	if (count==0)
		cout << endl << "No conversion was done due to an error" << "\a" << endl;
    return 0;
}



1-3 Enter a string to convert to Mo

Enter Morse Code(sperate by space):
B B B B A B B B A A B B A A A B A A
Press Enter to return to Quincy...
The overloaded bracket operator for a string returns the individual character in said string. This is what is happening with morsecode[i].When you test it in this loop...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int i=0; i<morsecode.size(); i++)
	{
		int find_string=0;
		while (find_string < 39)
		{
			if (morsecode[i]==morse[find_string][0])
			{
				count=1;
				cout << plaintext[find_string] <<" ";
				break;
			}
			find_string+=1;
		}
	}


...You are merely comparing the individual characters of the string with the individual characters of the morse array. Therefore, all the .'s match with A and all the -'s match with B.

There are many ways to do this, and I would go the route of parsing the input string morsecode into individual strings and testing them there one by one. I am a beginning programmer as well, and there are probably many better ways of doing it, but this would be my solution.

I should also point out that the break should probably be removed, as they are looked down upon and only used when absolutely necessary. An easy fix would be a boolean value test, where boolean "found" is initialized to false and changed to true if the if statement is true, like such

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool found;
int find_string;

...

for(int i=0; i<morsecode.size(); i++)
	{

		find_string=0;
                found = false;
		while (find_string < 39 && !found)
		{
			if (morsecode[i]==morse[find_string][0])
			{
				count=1;
				cout << plaintext[find_string] <<" ";
				found = true;
			}
			find_string+=1;
		}
	}


The integer count should also be made into a boolean value, and should probably be renamed. This simply makes the program more readable, and this variable is being used for a true/false test, so a boolean is the most efficient.

Don't forget to comment your code and get rid of unused variables. Readability is key.
Last edited on
u code does not work, but i fixed it....however, thx
Topic archived. No new replies allowed.