Question

Edited

Why I'm asking...

Yesterday, I started a project to create a Morse code translator which creates a file or appends to an existing file, translated Morse code from a given string or from the file text given.

Mainly,I have no idea in hell how to get this map to work with the string in which I want to return and I feel as if I've tried everything I can Google or read in documentation.

additionally...

I've left my horrendous attempt at iterating through the data structures , this time using vectors, having exhausted tries with map methods. I'm sure I'm missing simple syntx with the map structure but I left the last attempt up because I believe it conveys my intention quite clearly due to its baroque nature.

So to be more specific, what's the best way to access this map and return it through this function.

initial design

getTranslation()

/* @brief: Program returns string which is a translation
 * of the Rvalue string which it takes as a argument
 * @param text: string of letters, numbers and some symbols to be translated
 * @return translation: translated string appended with map values
 */
string getTranslation (const string&& text) noexcept(true)
{



//return value 
auto  translation = "";


map <string,string> morseKey;
morseKey ["A"] = ".-";
morseKey ["B"] = "-...";
morseKey ["C"] = "-.-.";
morseKey ["D"] = "-...";
//...

// I was going to attempt to
// unpack to vectors then compare as vectors of 
// strings because of consistent issues with
// type safety errors
// i've tried iterating over it differently
// but this is last hope code here
// any help on how to accomplish this in
// a better way but still retain the 
// use of a map because of its ability 
//to hold all sorts of characters
    //would be greatly appreciated

/*      
    vector <string> mSymbol;
    for (auto itr : morseKey)
    {   
        mSymbols.push_back(itr.first);


    }

    vector <string> vText;
    for (auto itr : text)
    {
    vText.push_back(itr);

    }


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

    if (vText[i] == mSymbol[i])
    {
    translation += morseKey.at(i);
    }


    }
*/

translation = "*SCAFFOLDING* FUNCTION NOT COMPLETE";
return translation;

}

Edit:

Wow, Iv'e received some really good input and I believe that my issues are rooted in the fact that using auto caused translation to be as a const char* which wouldn't allow me to make my map a map std::map<char,string> morseKey. Also my Rvalue cast via move was apparently unnecessarily (I had a feeling). So I'm going to implement the knowledge I've gained from and post that before I mark my answer.

Edit 2

  1. I removed the auto and 'translation' is now declared as a string

  2. getTranslation's signature takes a const string&

  3. I initialize morseKey as

static map <char,string> const morseKey = {{'A', ".-"},...

but get the compiler error of 'invalid conversion from ‘const char*’ to ‘const char&’ I don't understand why this is, or what makes either a pointer or ref in this situation and therefore how to fix it.

Was it helpful?

Solution

Wow... you practiced a lot of concepts, and you are just learning to code!!!

I'm sure you will be a successful programmer (but you should know it needs a lot more practicing!)

But about your "getTranslation" function, I changed it a little to:

#include <algorithm>
#include <cctype>
/*...*/

string getTranslation (string text) noexcept(true)
{
    map <char,string> morseKey;
    morseKey ['A'] = ".-";
    morseKey ['B'] = "-...";
    morseKey ['C'] = "-.-.";
    /*...*/

    string translation {""};
    std::transform(text.begin(), text.end(), text.begin(), toupper);
    for (it: text){
        translation += morseKey[it];
    }
    return translation;
}

As you may know, map is an associative array; it means you don't need to iterate over all of its element to find your interesting element. You should associate a key to its corresponding record. In your case you should associate a char (not a string) to a string; so you should define your "morseKey" as:

map <char, string> morseKey;

and when you want to associate a character such as 'A' to ".-" you should do something like:

morseKey ['A'] = ".-"; /*instead of morsKey["A"] = ".-" */;

also when you used "auto" in defining your "translation" variable, compiler will consider it as a "const char*". so you should explicitly define your "translation" variable as:

string translation {""};

In addition because our "morseKey" map contains just uppercase of alphabets, we should convert alphabet characters of "text" variable to uppercase. This can be done very easily by:

std::transform(text.begin(), text.end(), text.begin(), toupper);

but for using this command you should include two libraries:

#include <algorithm> /*for "transform" */
#include <cctype> /*for "touppper" */

Also you shouldn't consider "text" variable as rvalue any more (because we modify it) so I change your function delcarion to:

string getTranslation (string text) noexcept(true)  

Finally you should just iterate over your "text" variable and find corresponding Morse value of each character and append it to your return value; This also can be done very easily by:

    for (it: text){
        translation += morseKey[it];
    }

Have fun with programming!

My reply for your second edit:

I think your information is not enough; perhaps it's better to ask your question as a new question, and also provide it with more details, such as in which line you got this compile error or any other useful details that you may think it can be helpful.

OTHER TIPS

The function could be defined the following way. ( Note: I would make the map as a global variable in some namespace and initialize it with an array of std::pair(s) ).

std::string getTranslation( const std::string&& text) noexcept(true)
{
   std::string translation;

   std::map <std::string, std::string> morseKey;
   morseKey ["A"] = ".-";
   //...

   for ( char c : text )
   {
      c = std::toupper( c );

      auto it = morseKey.find( c );

      if ( it != morseKey.end() ) translation.push_back( it->second );
   }  

   return translation;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top