Can't Help Translating From Morse to Text

Hi Everyone,

I am fairly new to C++ and I have been
stuck on a problem for about a week now.

I was able to convert from text to morse,
but am having a difficult time going from
morse to text. I am only able to translate
one morse string into one character.

When I input the morse string i separate
each with a space
example: ".... .. ..-- .- .-.. .-.."
My output is only H

Below is an example of my code

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  include<iostream>
#include<iomanip>
#include<cctype>
#include<string>

using namespace std;
struct CODE{
	char letter;
	string code;
};

void Menu();
void MenuHeader();
void GenerateMorseCode( CODE x[] );
void TranslateMorse( CODE x[] );

int main(){
	CODE p[27] = { { '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',"--.." },{ ' ',"..--" } 
	};

	
	MenuHeader();
	Menu();
	GenerateMorseCode( p );
	TranslateMorse( p );

	system( "pause" );
	return 0;
}

void MenuHeader(){
	for(int i = 1; i <= 30; i++ ){
		if( i == 15 ) cout << "Menu";
		cout << char( 45 );
	}
	cout << endl;
}

void Menu(){
	string List[2] = { "Generate Morse Code","Translate Morse Code" };

	for( int i = 0; i < 2; i++ ){
		cout << (i+1) << left << setw(3) << '.' << List[i] << endl;
	}

}

void GenerateMorseCode( CODE x[] ){
	char Sentence;
	string MorseCode;

	cout << "Enter a Sentence: "; 
	
	 
	while(cin.get(Sentence), Sentence != '\n'){
		Sentence=toupper(Sentence);

		
		for( int i = 0; i < 27; i++ ){
			if( Sentence == x[i].letter ){ MorseCode += x[i].code+" "; }
		}
		
	}

	cout << right << setw(15) << "Morse Code:"<< right << setw(1) << MorseCode << endl;

}

void TranslateMorse( CODE x[] ){

	string Sentence;
	string MorseCode;

	cout << "Enter a Morse Code: ";
	cin >> MorseCode;

	for( int i = 0; i < 27; i++ ){

		if( MorseCode == x[i].code ) Sentence += x[i].letter;
	}

				
	cout << Sentence << endl;
}


Any Hints on how I can get more than one character out would be greatly
appreciated.

Thanks in advance
Maybe this will help you get started.

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

#include<iostream>
#include<iomanip>
#include<cctype>
#include<string>

using namespace std;

int main()
{
char str[]=".... .. ..-- .- .-.. .-..";
char c;
int i=0;

  while (str[i])
  {
    c=str[i];
    if (isspace(c))
	{c='\n';}
    putchar (c);
    i++;
  }

return 0;
}
Topic archived. No new replies allowed.