Search code examples
c++stringstlcontainersmorse-code

Trouble with morse code spaces between words


I am having trouble trying to get my code to convert a space character to 'xx'. I have it set so after every letter there is an x to separate the letters but I can't quite get what I have below to work for a space between words.

#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;

string translate(string word)
{
    string morseCode[] = { ".-x", "-...x", "-.-.x", "-..x", ".x", "..-.x",
    "--.x", "....x", "..x", ".---x", "-.-x", ".-..x", "--x", "-.x", "---x",
    ".--.x", "--.-x", ".-.x", "...x", "-x", "..-x", "...-x", ".--x", "-..-x",
    "-.--x", "--..x" };
    char ch;
    string morseWord = " ";
    //string morseWord = " " == "xx";


    for (unsigned int i = 0; i < word.length(); i++)
    {
        if (isalpha(word[i]))
        {
            ch = word[i];
            ch = toupper(ch);
            morseWord += morseCode[ch - 'A'];
            morseWord += morseCode[ch = ' '] == "xx";
            //morseWord += "xx";
            //morseWord += " " == "xx";
        }

    }
    return morseWord;
}

int main()
{
    stringstream stringsent;
    string sentence;
    string word = "";

    cout << "Please enter a sentence: ";
    getline(cin, sentence);
    stringsent << sentence;
    cout << "The morse code translation for that sentence is: " << endl;
    while (stringsent >> word)
        cout << translate(word) << endl;
    system("pause");
    return 0;
}



Solution

  • I've commented out all of the unnecessary bits.

    #include <iostream>
    // #include <cstring>
    // #include <sstream>
    #include <ccytpe>  // You were relying on an include dependency; this is the
                       // library that contains isalpha() 
    using namespace std;
    
    string translate(string word)
    {
        string morseCode[] = { ".-x", "-...x", "-.-.x", "-..x", ".x", "..-.x",
        "--.x", "....x", "..x", ".---x", "-.-x", ".-..x", "--x", "-.x", "---x",
        ".--.x", "--.-x", ".-.x", "...x", "-x", "..-x", "...-x", ".--x", "-..-x",
        "-.--x", "--..x" };
        char ch;
        string morseWord = " ";
        //string morseWord = " " == "xx";
    
    
        for (unsigned int i = 0; i < word.length(); i++)
        {
            if (isalpha(word[i]))
            {
                ch = word[i];
                ch = toupper(ch);
                morseWord += morseCode[ch - 'A'];
                // morseWord += morseCode[ch = ' '] == "xx";  // Having a space 
                                                              // character is 
                                                              // impossible here
                //morseWord += "xx";
                //morseWord += " " == "xx";
            }
            else if (isspace(word[i])) // True for any whitespace character
            {
                morseWord += "xx";
            }
    
        }
        return morseWord;
    }
    
    int main()
    {
        // stringstream stringsent;
        string sentence;
        // string word = ""; // should just be 'string word;' 
                             // Default constructed strings are already empty
    
        cout << "Please enter a sentence: ";
        getline(cin, sentence);
        // stringsent << sentence;
        cout << "The morse code translation for that sentence is: " << endl;
            cout << translate(sentence) << endl;
        return 0;
    }
    

    Your problem was two-fold. A space character is not alphabetic, so no space character could ever enter your if block. Secondly, in sending only one word at a time, you were never even sending space characters to begin with.

    Here's a sample output from the code above:

    Please enter a sentence: hello world
    The morse code translation for that sentence is: 
     ....x.x.-..x.-..x---xxx.--x---x.-.x.-..x-..x