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

Please help C++ lab For this lab you are required to create a class called \" CM

ID: 3855261 • Letter: P

Question

Please help C++ lab

For this lab you are required to create a class called "CMorseConvert" that converts a Text string into Morse Code (kind of). The class will not convert the text to dashes and dots but to the words "dash" and "dot".

As an example if the class is given the text string "1" the output would be "dot dash dash dash dash".

Or if the class is given the text string "123" the output would be "dot dash dash dash dash dot dot dash dash dash dot dot dot dash dash".

The CMorseConvert class will need to load the file "codes.dat" (which is already here, you just need to open it and read it in) with the mappings of the letters and numbers to the Morse Code. Here is a partial output of the "codes.dat" file:

A,.-

B,-...

C,-.-.

D,-..

E,.

The class needs to contain just one public function (you can have more but only one will be tested):

string Text2Morse(const string sText)

You can check your conversions using a real Morse Code Translator

I don't have access to codes.dat but you do not need it to write this code.

#include
using namespace std;

int main() {

/* Type your code here. */

return 0;
}

Explanation / Answer

Given below is the code for the question. It asks for a filename and passes that to constructor. The constructor also has a default value of "codes.dat" for filename in case you don't want to pass the filename. The code that are loaded are printed. You can comment out the line 72 where it prints out the codes , in case you don't want to see them. I just got a small set for alphabet and numbers and tested it. The program expects the file format to be

<character> <comma> <morsecode>

each line is without any spaces in between. If the format is different, the program will not work.

If there any issues running the code, post a comment. If happy with the answer , please rate it. Thank you.

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <cstring>

using namespace std;

typedef struct

{

char character; //the character which is to be encoded

string morsecode; // the morse code sequence containing words dash dot

}code;

class CMorseConvert{

code codes[100];

int size; //the number of elements in the codes array;

  

//convert a string such as .-. to dot dash dot

  

string convertCode(string morsecode)

{

string dashdot = "";

for(int i = 0; i < morsecode.length(); i++)

{

if(i != 0)

dashdot += " ";

if(morsecode[i] == '.')

dashdot = dashdot + "dot";

else if(morsecode[i] == '-')

dashdot = dashdot + "dash";

}

return dashdot;

}

  

//search the list of n codes for the character ch and return its morse code (dash dot sequence)

string getCode(char ch )

{

if(ch == ' ')

return " ";

//convert alphabet to uppercase

if(isalpha(ch))

ch = toupper(ch);

  

for(int i = 0; i < size; i++)

{

if(codes[i].character == ch)

return codes[i].morsecode;

}

return "NOCODE " ; //if character is not found

}

  

public:

//loads the file containing morse code into the array and returns the count

  

CMorseConvert(string filename = "codes.dat")

{

ifstream file(filename.c_str());

  

if(!file.is_open())

{

cout << "Could not open file " << filename << endl;

exit(1);

}

  

string line;

size = 0;

while(file >> line)

{

int idx = line.find(",");

char ch = line[idx-1]; //get the character before comma

string morsecode = line.substr(idx+1); //get the morse code after comma

if(isalpha(ch)) //convert alphabet to uppercase

ch = toupper(ch);

codes[size].character = ch;

codes[size].morsecode = convertCode(morsecode); //convert the morse code into a string of words dash and dot

cout << ch << " " << morsecode << " " << codes[size].morsecode << endl;

size ++;

  

}

  

}

  

  

string Text2Morse(const string sText )

{

string code = "";

for(int i = 0 ; i < sText.length(); i++)

{

if(i != 0)

code = code + " ";

code = code + getCode(sText[i]);

}

  

return code;

}

  

};

int main()

{

string filename;

string text , code;

  

cout << "Enter the filename containing morse codes: ";

getline(cin, filename);

  

CMorseConvert converter(filename);

  

  

cout << " Enter the text to be converted to morse code:" << endl;

getline(cin, text);

  

code = converter.Text2Morse(text);

  

cout << " The morse code for the give text is :" << endl;

cout << code << endl;

  

  

}

Input file: codes.dat

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,--..
0,-----
1,.----
2,..---
3,...--
4,....-
5,.....
6,-....
7,--...
8,---..
9,----.

output:

Enter the filename containing morse codes: codes.dat
A   .-   dot dash
B   -...   dash dot dot dot
C   -.-.   dash dot dash dot
D   -..   dash dot dot
E   .   dot
F   ..-.   dot dot dash dot
G   --.   dash dash dot
H   ....   dot dot dot dot
I   ..   dot dot
J   .---   dot dash dash dash
K   -.-   dash dot dash
L   .-..   dot dash dot dot
M   --   dash dash
N   -.   dash dot
O   ---   dash dash dash
P   .--.   dot dash dash dot
Q   --.-   dash dash dot dash
R   .-.   dot dash dot
S   ...   dot dot dot
T   -   dash
U   ..-   dot dot dash
V   ...-   dot dot dot dash
W   .--   dot dash dash
X   -..-   dash dot dot dash
Y   -.--   dash dot dash dash
Z   --..   dash dash dot dot
0   -----   dash dash dash dash dash
1   .----   dot dash dash dash dash
2   ..---   dot dot dash dash dash
3   ...--   dot dot dot dash dash
4   ....-   dot dot dot dot dash
5   .....   dot dot dot dot dot
6   -....   dash dot dot dot dot
7   --...   dash dash dot dot dot
8   ---..   dash dash dash dot dot
9   ----.   dash dash dash dash dot


Enter the text to be converted to morse code:
AppLe 123

The morse code for the give text is :
dot dash dot dash dash dot dot dash dash dot dot dash dot dot dot dot dash dash dash dash dot dot dash dash dash dot dot dot dash dash