0

I have been making a program that converts Letters, Numbers and Punctuations into morse code.

With the letters and numbers its working as I want it.

But with the punctuations I cant make it work properly. I was hoping someone could take a look at my code and help me.

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



    char ch;
    string morseWord = "";

    for(unsigned int i=0; i < word.length(); i++)
    {
        if(isalpha(word[i]))
        {
            ch ;
        }
    }
    return morseWord;
}


    char ch;
    string morseWord = "";

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

    string morseWord = "";

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



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

    code: " << endl;

    while(ss >> ToMorse(word) << endl;
        cout << PunctuationToMorse(word) << endl;
}
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Feb 11 '17 at 13:59
  • 1
    _"But with the punctuations i cant make it work properly."_ What's your actual problem? What is the input, the expected output and the actual output? – πάντα ῥεῖ Feb 11 '17 at 14:00
  • At the moment i want to be able to input: dot, question mark and eksklamation mark. When i input dot, it converts it fine. But when i input the other two it gives me: ?U???. – Lasse Hedegaard Feb 11 '17 at 14:04
  • 2
    Instead of mapping through direct array indices, you should provide a `std::map` to correlate the morse code strings to specific ASCII characters. That map could be used with a single function then. – πάντα ῥεῖ Feb 11 '17 at 14:07

1 Answers1

1

Your primary problem is that you missed to provide braces for the while() loop in your main() function:

while(ss >> word) { // <<<< Put an opening brace here
    cout << EnglishToMorse(word) << endl;
    cout << NumbersToMorse(word) << endl;
    cout << PunctuationToMorse(word) << endl;
} // <<<<< ... and a closing brace here

A generally better approach would be:

Map all known characters that can be converted to morse code using a std::map<char,std::string>, and have a single function to handle these:

string CharToMorse(char c) {
    static const std::map<char,std::string> morseCode = {
        { 'A', ".-" } ,
        { 'B' , "-..." } , 
        { 'C', "-.-." } ,
        // ...
        { 'Z', "--.." },
        { '0', ".----" } , 
        { '1', "..---" } , 
        { '2', "...--" } ,
        // ...
        { '9', "-----" } ,
        { ' ', "......." } // Consider to support spaces between words
        { '.', ".-.-.-" } ,
        { '!' , "..--.." } , 
        { '?' , "-.-.--"}
    };

    auto morseString = morseCode.find(toUpper(c));
    if(morseString != morseCode.end()) {
        return morseString->second;
    }
    return "";
}

and use it like:

int main() {   
    stringstream ss;
    string sentence;

    cout << "Enter a English word, number or punctuation: ";
    getline(cin, sentence);
    ss << sentence;
    cout << "Morse code: " << endl;
    char c;
    while(ss >> c) {
        cout << CharToMorse(c);
    }
    cout << endl;
}

The problem with your actual code is, that it makes assumptions relying on the ASCII character code table mappings, and that 'Z' - 'A' == 25.
This isn't guaranteed by the c++ standard and makes your code unportable (see here also).

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    The next evolution step would be to separate the underlying morse data from its representation. Instead of `std::string`, the mapped type of the `std::map` could be a class like `MorseCode`, which internally stores the series of "dots" and "hyphens" as e.g. a `private` data member of type `std::bitset`. And then provide a function like `std::string ToString(MorseCode const& morse_code)`. – Christian Hackl Feb 11 '17 at 15:14
  • @Christian Of course. Let's keep it simple for the newb. You're a real enthusiast ;-). Not to forget, with such class a `std::set` should be sufficient. – πάντα ῥεῖ Feb 11 '17 at 15:16
  • @LasseHedegard Stop editing my answer please. Improve your question instead. – πάντα ῥεῖ Feb 13 '17 at 17:26