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

This program needs to be written in C++. It must include 4 functions (including

ID: 641951 • Letter: T

Question

This program needs to be written in C++. It must include 4 functions (including main), use no global variables, dynamically create/delete the arrays, and close the file after usage.

This program must read in a mapping to Morse Code from a given file. The program should prompt the user to input a file name, then check for its existence. The program should then open this file and read the values into two arrays. One array will contain the characters that are being mapped from and the second array will contain the equivalent Morse code. Once the arrays have been built the program will read in text from cin and translate it into Morse code. Then ask the user to insert a line of text. The program should convert the text to all caps (toupper) and then convert the string to Morse Code. The program will continue prompting the user to enter additional lines to be translated until the user enters "quit." The program should also close if the given file does not exist.

The Morse Code translation must include a white space between each output character. It should read back symbols that don't have Morse Code equivalents in quotes. i.e., if the user enters a dollar sign, $, the program should output it as "$". That is, the program should identify that the character is not included in the mapping and should return it in quotes.

Thank you!

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void openFile(ifstream &in){
   string fName;
   cout << "Enter file Name: ";
   cin >> fName;
   in.open(fName.c_str());
}

void readFile(ifstream &in, string *arr){
   char ch;
   string morse;
   while(in >> ch >> morse){
       arr[ch] = morse;
   }
}

string convertToMorse(string *arr, string text){
   string temp = "";
   for(int i = 0; i < text.size(); ++i){
       if(arr[toupper(text[i])] != ""){
           temp += arr[toupper(text[i])];
       }
       else{
           temp += text[i];
       }
       temp += " ";
   }
   return temp;
}

void getUserInput(string *arr){
   string text, option, temp;
   do{
       cout << "Enter any line to convert to morse: ";
       getline(cin, text);
       getline(cin, text);
       temp = convertToMorse(arr, text);
       cout << "morse of " << text << " is " << temp << endl;
       cout << "Do you want to try another(Enter 'yes' for yes): ";
       cin >> option;
   }while(option == "yes" || option == "YES" || option == "Yes");
}

int main(){
   ifstream in;
   string option, text;
   string temp;
   string *arr = new string[256];
   for(int i = 0; i < 256; ++i){
       arr[i] = "";
   }
   openFile(in);
   if(in.is_open()){
       readFile(in, arr);
       getUserInput(arr);
   }
   else{
       cout << "Can not open the file" << endl;
   }
   delete[] arr;
   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