Link to home
Start Free TrialLog in
Avatar of kennyu125
kennyu125

asked on

C++ morse code converter

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

ASKER CERTIFIED SOLUTION
Avatar of rendaduiyan
rendaduiyan
Flag of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

.... 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

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.