Sign in to follow this  

Morse code to text: sentences.

This topic is 2537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I've been working on my assignment for a couple of days, got most of it done. The assignment is to code a program that translates single words to and from morse code. I decided I'd try to make it whole sentences. Now, I have managed to translate text sentences to morse, but I'm not sure how to do it the other way around. Here is the code I use to translate a word from morse to text:


while (i < strlen)
{
for (; myString[i] != ' '; i++)
myMorse += myString[i];
for (k = 0; k < 26; k++)
if (myMorse == morseCode[k])
myOutput += alphabet[k];
i++;
myMorse.clear();
}
if (myOutput.length())
cout << myOutput << endl;



I was thinking of using something like this:


if (myString[i] == '|')
myOutput += " ";



But I'm kind of stumped as where to put it. Might be having a temporary brainfreeze (I've been programming for around 14 hours each day the past 4-5 days). Getting kind of tired XD Am I on the right track? What should I keep in mind? Any helpful hints?

Note: PLEASE do not post a ready solution, doing the work for me is less productive than letting me think for myself.

Thank you in advance for your time! =)

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>
using namespace std;

int main()
{
int strlen;
bool flag = false;
string myString, myMorse, myOutput;
string alphabet[] = { "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 morseCode[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-", ".--", "-..-", "-.--", "--.." };
while (flag == false)
{
getline(cin, myString);
strlen = myString.size();

if (myString == "quit0")
{
cout << "CHANGE!" << endl;
flag = true;
break;
}
for (int i = 0; i < strlen; i++)
{
myString[i] = tolower(myString[i]);

if (myString[i] == ' ')
cout << '|' << " ";
else
cout << morseCode[myString[i] - 'a']<<' ';
}
cout << endl;
}

flag = false;
while (flag == false)
{
myOutput.clear();
getline(cin, myString);
myString += ' ';
strlen = myString.size();
int i = 0, k = 0;

if (myString == "quit0 ")
{
cout << "BREAK!" << endl;
flag = true;
break;
}
while (i < strlen)
{
for (; myString[i] != ' '; i++)
myMorse += myString[i];
for (k = 0; k < 26; k++)
if (myMorse == morseCode[k])
myOutput += alphabet[k];
i++;
myMorse.clear();
}
if (myOutput.length())
cout << myOutput << endl;
}
}



Share this post


Link to post
Share on other sites
The text->morse direction is a simple replacement operation, since each of the items you are replacing are the same length (1 character) and unique. The morse->text direction is a bit harder, because the items you want to replace are of variable length.

The simplest solution is to do the replacement by 'phrase', rather than by character. You can split the input string wherever a space character occurs, and then check the phrase (the sequence of characters between two sets of spaces) against all your possible choices.

I don't know if you have learned about it yet, but std::map would make your life a lot easier for this.

Share this post


Link to post
Share on other sites
I'm using this as a solution:


morseOutput.clear();
getline(cin, String);
String += ' ';
strlen = String.size();
int i = 0, k = 0;

while (i < strlen)
{
while (String[i] != ' ' && String[i] != '|')
{
morseString += String[i];
i++;
}
for (k = 0; k < 26; k++)
if (morseString == morseCode[k])
morseOutput += alphabet[k];
if (String[i] == '|')
morseOutput += ' ';
i++;
morseString.clear();
}
if (morseOutput.length())
cout << morseOutput << endl;





It works alright. Overall I think my code is a little crap, it could be better, but it's getting tight, gotta turn it in tomorrow, girlfriend isn't very happy with me programming for 14 hours a day, and I still have to read up on pointers before next week. Plus I have a final exam in the course.

EDIT: Nevermind, solved it.

[Edited by - Metallon on December 30, 2010 2:34:44 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
]Obsessive programming + relationship = !relationship. Make sure that programming doesn't take over your life [smile]

Haha this made me laugh out loud ^_^ But it's a bit a necessity since it's part of my course, and I was slacking a bit last week (as in, not being as thorough with my studies as I was supposed to), so I had to catch up this week. They're three classes (the first is one is two weeks, the second is three weeks and the third is five weeks) but I need to do them in six weeks.

Anyway, I think my teacher can accept that I left out these special characters, once I explain the problem. Like I said, time is against me and I have this weekend to pick up on pointers because the last class starts on Monday and I want pointers in the bag so I can focus on what that course is mainly going to be about, which is OOP.

EDIT: Now something that used to work perfectly fine is not working. Even though I haven't changed a single line of code. Bullshit.

Share this post


Link to post
Share on other sites

This topic is 2537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this