[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C++ morse code converter

Posted on 2009-04-26
4
Medium Priority
?
9,773 Views
Last Modified: 2013-12-14
Is there a more efficient way to translate the characters to morse code instead of using this many if/else if statements?
#include <iostream>
#include <cstring>
using namespace std;
 
 
void engconvert (char[50]);
 
int main()
{
char englstring[50];
 
cout <<"Enter the English text which you would like to be converted to Morse Code: \n";
 
cin >> englstring;
 
engconvert(englstring);
 
system ("pause");
return 0;
}
 
void engconvert (char english[])
{
	int englishstring2;
 
	englishstring2 = strlen (english);
 
	cout << englishstring2 << endl;
 
	cout << "Here is the morse code translation:\n";
 
	for (int i = 0; i<englishstring2; i++)
 
{
	if (english[i] == ' ')
		cout << endl;
	else if (english[i] == ',')
		cout << "--..--" << endl;
	else if (english[i] == '.')
		cout << ".-.-.-" << endl;
	else if (english[i] == '?')
		cout << "..--.." << endl;
	else if (english[i] == '0')
		cout << "-----" << endl;
	else if (english[i] == '1')
		cout << ".----" << endl;
	else if (english[i] == '2')
		cout << "..---" << endl;
	else if (english[i] == '3')
		cout << "...--" << endl;
	else if (english[i] == '4')
		cout << "....-" << endl;
	else if (english[i] == '5')
		cout << "....." << endl;
	else if (english[i] == '6')
		cout << "-...." << endl;
	else if (english[i] == '7')
		cout << "--..." << endl;
	else if (english[i] == '8')
		cout << "---.." << endl;
	else if (english[i] == '9')
		cout << "----." << endl;
	else if (english[i] == 'A' || english[i] == 'a')
		cout << ".-" << endl;
	else if (english[i] == 'B' || english[i] == 'b')
		cout << "-..." << endl;
	else if (english[i] == 'C' || english[i] == 'c')
		cout << "-.-." << endl;
	else if (english[i] == 'D' || english[i] == 'd')
		cout << "-.." << endl;
	else if (english[i] == 'E' || english[i] == 'e')
		cout << "." << endl;
	else if (english[i] == 'F' || english[i] == 'f')
		cout << "..-." << endl;
	else if (english[i] == 'G' || english[i] == 'g')
		cout << "--." << endl;
	else if (english[i] == 'H' || english[i] == 'h')
		cout << "...." << endl;
	else if (english[i] == 'I' || english[i] == 'i')
		cout << ".." << endl;
	else if (english[i] == 'J' || english[i] == 'j')
		cout << ".---" << endl;
	else if (english[i] == 'K' || english[i] == 'k')
		cout << "-.-" << endl;
	else if (english[i] == 'L' || english[i] == 'l')
		cout << ".-.." << endl;
	else if (english[i] == 'M' || english[i] == 'm')
		cout << "--" << endl;
	else if (english[i] == 'N' || english[i] == 'n')
		cout << "-." << endl;
	else if (english[i] == 'O' || english[i] == 'o')
		cout << "---" << endl;
	else if (english[i] == 'P' || english[i] == 'p')
		cout << ".--." << endl;
	else if (english[i] == 'Q' || english[i] == 'q')
		cout << "--.-" << endl;
	else if (english[i] == 'R' || english[i] == 'r')
		cout << ".-." << endl;
	else if (english[i] == 'S' || english[i] == 's')
		cout << "..." << endl;
	else if (english[i] == 'T' || english[i] == 't')
		cout << "-" << endl;
	else if (english[i] == 'U' || english[i] == 'u')
		cout << "..-" << endl;
	else if (english[i] == 'V' || english[i] == 'v')
		cout << "...-" << endl;
	else if (english[i] == 'W' || english[i] == 'w')
		cout << ".--" << endl;
	else if (english[i] == 'X' || english[i] == 'x')
		cout << "-..-" << endl;
	else if (english[i] == 'Y' || english[i] == 'y')
		cout << "-.--" << endl;
	else if (english[i] == 'Z' || english[i] == 'z')
		cout << "--.." << endl;
	else
	{
		cout << "You have not entered a character in the american";
	    cout << " alphabet. Please re-run the program and try again." << endl;
	}
 
 
 
}
cout <<endl;
 
}

Open in new window

0
Comment
Question by:kennyu125
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
rendaduiyan earned 2000 total points
ID: 24239055
You can setup a dictionary for it by a 2-dimension array, say,
typedef struct Morse
{
   char english;
   char* morse_string;
} MorsePeer;

struct MorsePeer lookup[] = {'1',".----"}, ,,,};
0
 
LVL 19

Expert Comment

by:alb66
ID: 24239262
Ypu can use a switch statement:
        switch( english[i] )
        { 
           case ' ':  cout << endl;   break;
           case ',':  cout << "--..--" << endl; break;
           case '.':  cout << ".-.-.-" << endl; break;
           case '?':  cout << "..--.." << endl; break;
        else if (english[i] == '0')
                cout << "-----" << endl;

Open in new window

0
 
LVL 19

Expert Comment

by:alb66
ID: 24239276
.... I start submit while I was still typing...-


        
        bool bOK = true;
 
        switch( english[i] )
        { 
           case ' ':  break;
           case ',':  cout << "--..--"; break;
           case '.':  cout << ".-.-.-"; break;
           case '?':  cout << "..--.."; break;
           case '0':  cout << "-----";  break;
           ...
           default: bOK = false; break;
        }
 
        if ( !bOK ) 
        {
          cout << "You have not entered a character in the american";
          cout << " alphabet. Please re-run the program and try again.";
        }
        cout << endl;

Open in new window

0
 
LVL 1

Expert Comment

by:jefftope
ID: 24240279
I would suggest creating a helper class, utilising std::map<char,std::string> or similar as the container. Populate it once using either insert or []. You can then use std::find for output, wrapping in methods as appropriate.

I would also suggest, only using a single cout, except for your error condition. Try using a helper function, you can return an empty string for invalid and test it as error condition, (or  a bool function etc.). If you are then asked to reformat the output, you will have far less places to change it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question