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

I need help coding this out. I am struggling with opening the txt file and manip

ID: 3676297 • Letter: I

Question

I need help coding this out. I am struggling with opening the txt file and manipulating the text in order to translate it.

THis is the text within the txt file

- .... . / ... - .- - ..- - . / .-- .- ... / .- / -. --- - .- -... .-.. . / .--. .-. . -.-. ..- .-. ... --- .-. / --- ..-. / - .... . / . ... - .- -... .-.. .. ... .... -- . -. - / -.-. .-.. .- ..- ... . / .- -. -.. / ..-. .-. . . / . -..- . .-. -.-. .. ... . / -.-. .-.. .- ..- ... . / --- ..-. / - .... . / ..-. .. .-. ... - / .- -- . -. -.. -- . -. - / - --- / - .... . / ..- -. .. - . -.. / ... - .- - . ... / -.-. --- -. ... - .. - ..- - .. --- -.

Engineering Problem Solving II Spring 2016 Homework #4 Due March 11,2016 a 5:00 pm Morse code, named after artist and inventor Samuel F. B. Morse (1791-1872), was often used when one dusty sheriff needed to inform another dusty sheriff that the bad guys were coming to town. The code is presently used by many, including Ham radio operators, and the airlines industry (when modern radio communications fail - it's true!). The code consists of dots and dashes. Our EPSII Morse code message in file 'HW4_Morse.txt' consists of using .' for a dot, and '-' for a dash. White spaces are used to separate letters, and words are separated by 7" Here are your tasks for this homework: 1. Open the file named 'HW4 Morse.txt' for reading. a. Translate the Morse code in that file into a message in the English language. b. Display the English message on the console. c. Write the translated message to a file named 'message.txt'. After writing the file, close the file. The contents of this file should be in English 2. Now open the file 'message.txt' for reading. a. Read the English text from the file, and translate each letter in the text into Morse code. b. Write the code, letter by letter, to a new file called 'message2.txt' and to the console. c. When the last letter in the text has been translated and written in Morse code, close the output file. 3. To ensure your program ran correctly, open 'message2.txt' and 'HW4 _Morse.txt' and, by eye, check that the contents are exactly the same. 4. Submit your source code (*.cpp) to the class's ICON site dropbox by 5:00PM, March 11, 2016. Hint: Recall from Lecture Notes 4 (slide 22) and the textbook (pp. 67-68), that cin can be used as cin.get(ch1) to get a single character from the input and store it in the character variable called ch1. You can use the .get() function with an ifstream variable to read a single character from a file in the same manner.

Explanation / Answer

Comments added

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <sstream>
using namespace std;
string englishToMorse (string, string[]);
string morseToEnglish (string, string[]);

int main ()
{
string morseText[26] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
string englishText, morsecodeText;
  
//reading file and convert morse to english
std::ifstream readfile("HW_4 Morse.txt");//reding numbers from text file
std::string eachline;
while (std::getline(readfile, eachline))
{
std::istringstream iss(eachline);
morsecodeText = morsecodeText + eachline;
}

//outputting to file
ofstream out_data("message.txt");
out_data << " English message is: "<<morseToEnglish(morsecodeText,morseText);
//outputText to console
cout << " English message is: "<<morseToEnglish(morsecodeText,morseText);


//2. Now reading english and converting to morse
std::ifstream readfile1("message2.txt");//reding numbers from text file
while (std::getline(readfile1, eachline))
{
std::istringstream iss(eachline);
englishText = englishText + eachline;
}

//outputting to file
ofstream out_data1("HW_4 Morse.txt");
out_data1 << "Morse message is: "<<englishToMorse(englishText,morseText);
cout<< "Morse message is: "<<englishToMorse(englishText,morseText);
return 0;
}

string englishToMorse (string englishText, string morseText[])
{
int totalCharacters = englishText.length();
string morseConvertedValue;
string letterSpaces= " ";
string wordsSpaces = " ";//adding extra space to make three
for (int k=0; k<totalCharacters; k++)
{
if (englishText[k]!= ' ') //check until space founds
{ englishText[k]=toupper(englishText[k]); //making case insensitive
morseConvertedValue=letterSpaces+=morseText[englishText[k]-'A']+" ";//converting each morseLetter
}
if (englishText[k]==' ')
{
letterSpaces+=wordsSpaces;//adding three spaces
}
}
return morseConvertedValue;
}

string morseToEnglish(string morsecodeText, string morseText[])
{
//variables declared
int const totalCharacters = 26;
std::string outputText = "";
int temp = 0;
while(temp != morsecodeText.size())
{
//getting morse code
std::string morseLetter = "";
while(morsecodeText[temp] != ' ')
{
morseLetter += morsecodeText[temp++];
}
//getting morse alphabet
int pos = 0;
while(morseText[pos] != morseLetter && pos < 26)
{++pos;
outputText += 'A' + pos;
++temp; //to handle extra space
}
}

return outputText;
}

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