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

For this lab you are required to create a class called \" CMorseConvert \" that

ID: 3866412 • Letter: F

Question

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:

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



You can check your conversions using a real Morse Code Translator

I keep getting errors in the code saying "error: expected unqualified-id before 'public' public : "

Code that I already have:

#include<iostream>
#include<string>
#include<fstream>
#include<stdio.h>
using namespace std;
typedef struct{
   char character;
   string morsecode;
}Code;

class CMorseConvert{
   Code codes[100];
   int count;
   string convertCode(string morsecode){
      string dashdotString = "";
      int len = morsecode.length();
         for(int i = 0; i < len; i++){
         if(morsecode[i] == '.'){
            dashdotString = dashdotString + "dot "+" ";
         }
         else if(morsecode[i] == '-'){
            dashdotString = dashdotString + "dash "+" ";
         }
         else{
            dashdotString = dashdotString +morsecode[i];
         }
      return dashdotString;
      }
   }
   string getCode(char ch ){
      if(ch == ' '){
         return " ";
      }
      else if(isalpha(ch)){
         ch = toupper(ch);
      }
      for(int i = 0; i < count; i++){
         if(codes[i].character == ch){
            return codes[i].morsecode;
         }
      }
      return "NOCODE";
   }
}
public :
   CMorseConvert(){
      ifstream morseFile("codes.txt");
      if(!morseFile.is_open()){
         cout<<"Could not open morseFile ";
         exit(1);
      }
      string line;
      count = 0;
      while(morseFile >> line){
         int index = line.find(",");
         char ch = line[index-1];
         string morsecode = line.substr(index+1);
         if(isalpha(ch)){
            ch = toupper(ch);
            codes[count].character = ch;
            codes[count].morsecode = convertCode(morsecode);
            count ++;
         }
         morseFile.close();
      }
      string Text2Morse(const string sText ){
         string morseCode = "";
         int len = sText.length();
        for(int i = 0 ; i < len; i++){
           morseCode = morseCode + getCode(sText[i]);
            return morseCode;
       }
      }
   }
};
int main(){
   string stext , mcode;
   CMorseConvert converter;
   cout << "Enter the text to be converted to morse code:";
   getline(cin, stext);
   mcode = converter.Text2Morse(stext);
   cout << mcode << endl;
}

Explanation / Answer

As you requested I have fixed the syntax errors you made. I hope this is the only thing you requested. Let me know if you need anythong more!

#include<iostream>
#include<string>
#include<fstream>
#include<stdio.h>
#include<cstdlib>
using namespace std;
typedef struct{
   char character;
   string morsecode;
}Code;

class CMorseConvert{
   Code codes[100];
   int count;
   string convertCode(string morsecode){
      string dashdotString = "";
      int len = morsecode.length();
         for(int i = 0; i < len; i++){
         if(morsecode[i] == '.'){
            dashdotString = dashdotString + "dot "+" ";
         }
         else if(morsecode[i] == '-'){
            dashdotString = dashdotString + "dash "+" ";
         }
         else{
            dashdotString = dashdotString +morsecode[i];
         }
      return dashdotString;
      }
   }
   string getCode(char ch ){
      if(ch == ' '){
         return " ";
      }
      else if(isalpha(ch)){
         ch = toupper(ch);
      }
      for(int i = 0; i < count; i++){
         if(codes[i].character == ch){
            return codes[i].morsecode;
         }
      }
      return "NOCODE";
   }
public :
   CMorseConvert(){
      ifstream morseFile("codes.txt");
      if(!morseFile.is_open()){
         cout<<"Could not open morseFile ";
         exit(0);
      }
      string line;
      count = 0;
      while(morseFile >> line){
         int index = line.find(",");
         char ch = line[index-1];
         string morsecode = line.substr(index+1);
         if(isalpha(ch)){
            ch = toupper(ch);
            codes[count].character = ch;
            codes[count].morsecode = convertCode(morsecode);
            count ++;
         }
      }
         morseFile.close();
   }
      string Text2Morse(const string sText ){
         string morseCode = "";
         int len = sText.length();
        for(int i = 0 ; i < len; i++){
           morseCode = morseCode + getCode(sText[i]);
            return morseCode;
       }
   }
};
int main(){
   string stext , mcode;
   CMorseConvert converter;
   cout << "Enter the text to be converted to morse code:";
   getline(cin, stext);
   mcode = converter.Text2Morse(stext);
   cout << mcode << endl;
}

Dont forget to comment!

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