Converting English to Morse Code - Logic Debugging

Hello all, I have a fairly simple problem with a program I am writing that converts an English statement into Morse code. The problem I am having is the relationship between string arrays, strings, and chars. When I last compiled, it output the first few of my test words in English and crashed at runtime.

I am not sure exactly how to pass the Morse code value in the string[] into my word I am making. I also think I have a problem with breaking the string into words, but I can't be sure until I get this piece to work.

Thanks for any and all help, I really appreciate it.

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
#include <iostream>
#include <cstring>
using namespace std;

string translate(string word)
{
	string morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.", 
	"--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", 
	".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", 
	"-.--", "--.."};
	char ch;
	string morseWord = "";
	
	for(unsigned int i=0; i < word.length(); i++)
	{
		ch = word[i];
		ch = toupper(ch);
		morseWord += morseCode[ch - 'A'];
		morseWord += " ";
	}
	return morseWord;
}

int main()
{	
	string sentence;
	string word = "";
	int currentLoc = 0; //Picks up after the last word
	int index = 0; //Counter along the string
	cout << "English: ";
	getline(cin, sentence);
	
	for(unsigned int i = 0; i < sentence.length(); i = currentLoc)
	{
		currentLoc = index;
		
		for(unsigned int j = 0; j < sentence.find(" ", currentLoc); j++)
		{
			if(isalpha(sentence[j]))
				word += sentence[j];
			
			index++;
		}
		cout << word << endl;
		word.clear();
	}
}
Wow. I'm sorry I'm an idiot. Well obviously it was printing in English because I didn't call my function translate(). I fixed the code so it reads:

cout << translate(word) << endl;

So now, with a test input of: "Hey, this is a test."

Instead of outputting:

1
2
3
Hey
Hey
Heythis 


and crashing, it outputs the Morse code equivalent of the above statement and crashes.

Thanks in advance for any replies though.
So, in other words, I have a problem breaking up a string into words.
Just for closure, I actually found a solution using the <sstream> library. I'll just leave the source here if anyone wants to look at it.

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
#include <iostream>
#include <cstring>
#include <sstream>
using namespace std;

string translate(string word)
{
	string morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.", 
	"--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", 
	".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", 
	"-.--", "--.."};
	char ch;
	string morseWord = "";
	
	for(unsigned int i=0; i < word.length(); i++)
	{
		if(isalpha(word[i]))
		{
			ch = word[i];
			ch = toupper(ch);
			morseWord += morseCode[ch - 'A'];
			morseWord += " ";
		}
	}
	return morseWord;
}

int main()
{	
	stringstream ss;
	string sentence;
	string word = "";
	
	cout << "English: ";
	getline(cin, sentence);
	ss << sentence;
	cout << "Morse: " << endl;
	
	while(ss >> word)
		cout << translate(word) << endl;
}
Last edited on
Topic archived. No new replies allowed.