Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There are two problems with my C++ code. This is about fromMorse to English text and English text to Morse code. a) There are some

There are two problems with my C++ code. This is about fromMorse to English text and English text to Morse code.

a) There are some warnings.

b) The validation when I convert from Morse Code to Englishtext, I have to input the Morse Code only.

Here is my code: Help me to fix it to function properly.Thanks!

#include
#include
#include

using namespace std;

string engToMorse(string, string const[]);
string morseToEng(string, string const[]);

int main()
{
string const morse[27] =
{ ".-", "-...", "-.-.", "-..", ".","..-.", "--.", "....", "..", ".---", "-.-",
".-..", "--", "-.", "---", ".--.", "--.-",".-.", "...", "-", "..-", "...-",
".--", "-..-", "-.--", "--.." };
string text, morseCode;
char choice;
char repeat = 'y';

while (repeat == 'y' || repeat == 'Y')
{
cout << " Select 1 toencode English text to Morse code. Select 2 to decode Morse codeto English text.";
cout << endl <<"Please make your selection: ";
cin >>choice; cin.ignore(80, '');

if (choice == '1')
{
cout<< " NOTE. DO NOT INPUT A NON ENGLISH CHARACTER."
<< " THIS TRANSLATOR EXCLUSIVELYTRANSLATES ENGLISH TEXTS (CAPITALIZED AND NONCAPITALIZED).";
cout<< " Enter text to translate, each word seperated by a spaceif you want to translate more than one word: ";
cin.get();
getline(cin, text);

cout<< " TEXT: " << text << endl;
cout<< " MORSE CODE: " << engToMorse(text, morse) < }

else if (choice =='2')
{
cout
<< " Enter a morsecode to translate,each letter code seperated by a space."
<< " If you want to translate morethan one word, have 2 spaces between each word."
<< "(for example, ... --- ... ... --- ...): ";
cin.get();
getline(cin, morseCode);

cout<< " MORSECODE: " << morseCode << endl;
cout<< " TEXT: " << morseToEng(morseCode, morse) < }

else
cout<< " INVALID SELECTION " << endl;
cout << " Would youlike to continue? Press y to repeat. Press any other key to exit.";
cin >> repeat;
}

system("Pause");
return 0;
}

string engToMorse(string text, string const morse[])
{
int textLength = text.length();
string morseValue;
string spacesBtwLetters = " ";
string spacesBtwWords = " "; //1 space betweenwords

for (int k = 0; k < textLength; k++)
{
if (text[k] != ' ') //if theword(s) did not encounter a space
{
text[k] =toupper(text[k]);
//uppercase letters and lower case letters are the same hence have thesame appropriate morse code.

morseValue += morse[text[k] - 'A'] + spacesBtwLetters;
//A is thefirst value of the array.
}//By subtracting its findingthe appropriate morse code for each letters

if (text[k] == ' ')
{
morseValue+= spacesBtwWords; // 2 spaces when there is a space betweenwords
}

if (text[k] < 'A'&& text[k] < 'Z' && text[k] != ' ')
{
morseValue= " INVALID CHARACTER USED ";
}
}

return morseValue;
}

string morseToEng(string morseCode, string const morse[])
{
int const characters = 26;
string output = ""; //output for function
int index = 0;

while (index < morseCode.size())
{
//find current morsecode
string letter = "";

while (morseCode[index] !=' ' && index < morseCode.size())
{
letter +=morseCode[index++];
}

//find this code in themorse alphabet
int position = 0;

while (morse[position] !=letter && position < 26)
{
++position;
}

if ('A' + position =='[')
output +=' ';
else
output +='A' + position;
++index; //to get rid of thespace character.
}

return output;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions