Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

10.17: Morse Code Converter (C++) (Please fix my code) Morse code is a code wher

ID: 3830562 • Letter: 1

Question

10.17: Morse Code Converter (C++) (Please fix my code)

Morse code is a code where each letter of the English alphabet, each digit, and various punctuation characters are represented by a series of dots and dashes. Table 10-8 from the textbook shows part of the code.


Write a program that asks the user to enter a string , and then converts that string to Morse code. Note that Morse code represents both upper and lower case letters so that both 'A' and 'a' will be converted to ".-".

Input Validation.
None.

This is what I have so far:

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


string texttomorse(char c)
{
string text = "abcdefghijklmnopqrstuvwqyz"; //osv
string morse[] = {".-","-...","-.-.","-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--.."}; //osv
int index = text.find(c);
if(index!=-1)
return morse[index];
else
return " ";
}

int main()
{
string ord;
getline(cin, ord);
string morse="";
for(int i=0; i<ord.length(); i++)
{
morse += texttomorse(ord[i]);
}
cout << morse << endl;

return 0;
}

And this is the error I'm recieving:

Problems Detected: The contents of your standard output is incorrect. Given the following was entered from the keyboard: What hath God wrought? you displayed instead of: Enter a word and I will translate it to Morse code:

Explanation / Answer

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

string texttomorse(char c)
{
if(c>='A' && c<='Z')
{
   c = c-'A'+'a';
}  
string text = "abcdefghijklmnopqrstuvwqyz"; //osv
string morse[] = {".-","-...","-.-.","-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--.."}; //osv
int index = text.find(c);
if(index!=-1)
   return morse[index];
else if(c == '?')
   return "..--..";
else if(c == ''')
   return ".----.";
else if(c == '"')
   return ".-..-.";
else if(c == ''')
   return ".----.";
else if(c == '(' || c==')')
   return "-.--.-";
string out = "";
   switch(c)
   {
           case '0' :
{
out = "----- ";
break;
}
case '1' :
{
out = ".---- ";
break;
}
case '2' :
{
out = "..--- ";
break;
}
case '3' :
{
out = "...-- ";
break;
}
case '4' :
{
out = "....- ";
break;
}
case '5' :
{
out = "..... ";
break;
}
case '6' :
{
out = "-.... ";
break;
}
case '7' :
{
out = "--... ";
break;
}
case '8' :
{
out = "---.. ";
break;
}
case '9' :
{
out = "----. ";
break;
}
}
return out;
}
int main()
{
string ord;
cout<<"Enter a word and I will translate it to Morse code: ";
getline(cin, ord);
string morse="";
for(int i=0; i<ord.length(); i++)
{
morse = texttomorse(ord[i]);
cout<<morse<<endl;
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote