Morse Code Translator

Ben Jorden 6 Reputation points
2022-01-06T10:28:22.553+00:00

For an assignment, I'm unable to use the strings. Only c-style strings. Any help in solving this problem would be highly appreciated.

Source: Online Morse Code Translator

a5.cpp:45:10: error: invalid operands to binary expression ('char [1]' and 'int')

output += ' ';

~~~~~~ ^ ~~~

a5.cpp:59:27: error: excess elements in char array initializer

char morse_code[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.-...

^~~~~~

a5.cpp:75:16: error: invalid operands to binary expression ('char [255]' and 'int')

output += alpha_num[position];

~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~

a5.cpp:82:10: error: invalid operands to binary expression ('char [255]' and 'int')

output += ' ';

~~~~~~ ^ ~~~





#include <cstdlib>

#include <istream>

using namespace std;

void engtomol(char *text);

char engtomol(char *text){

char *morse_code[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};

char alpha_num[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};

size_t const characters = 36;

char message[255];

char output[]="";

size_t index = 0;

size_t const size = sizeof(message);

while (index < size){

int position = 0;

if (message[index] != ' '){

while (alpha_num[position] != message[index] && position < 36){

++position;

}

output += morse_code[position];

}

else{

output += ' ';

}

}

return 0;

}

char moltoeng(char* morsecode){

char morse_code[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};

char alpha_num[36]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};

size_t const characters = 36;

char output[255];

size_t index = 0;

size_t const size = sizeof(morsecode);

while(index < size){

string letter = "";

while(morsecode[index] != ' ' && index < size){

letter += morsecode[index++];

}

//find this code in the morse alphabet

int position = 0;

while(morse_code[position] != letter && position < 36){

++position;

}

output += alpha_num[position];

int count = 0;

while(morsecode[index] == ' '&& index < size){

index++;

count++;

}

if(count >= 7){

output += ' ';

}

}

return 0;

}

int main()

{

char *text;

char *morsecode;

int choice = 0;

char repeat = 'y';

while (repeat == 'y'){

cout<<"do you want to translate English to Morse code(1) or translate Morse code to English (2)?"<<endl;

cin>>choice;

if(choice==1){

cout<<"you have selected English to morse." << endl;

cout<<"Please type in a text you would like translated "<< endl;

cin.get();

cout<< "Text:" << text << endl;

cout<< "Morsecode: " << engtomol(text) <<endl;

}

else if(choice==2){

cout<<"You have selected morse to english." << endl;

cout<<"Please type in morse code(1 space between . and - within a character, 3 spaces between characters and 7 spaces between words)"<< endl;

cin.get();

cout<< "Morsecode:" << morsecode << endl;

cout << "Text: " << moltoeng(&morsecode) << endl;

}

else if ((choice < 1)||(choice > 2)){

cout << "invalid choice"<<endl;

}

{

cout << " Would you like to continue? Press y to repeat. Press any other key to stop.";

cin>>repeat;

}

}

return 0;

}
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,913 questions
{count} vote

3 answers

Sort by: Most helpful
  1. WayneAKing 4,926 Reputation points
    2022-01-07T01:32:59.05+00:00

    For an assignment, I'm unable to use the strings.
    Only c-style strings.

    From where did the C++ code you posted come? It seems
    unlikely that you wrote all of it.

    a5.cpp:59:27: error: excess elements in char array initializer

    char morse_code[36]={".-","-...","-.-.","-..",".","..-.","--.",
    "....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",
    ".-.","...","-","..-","...-",".--","-..-","-.-...

    You have declared an array of chars, but the initializers
    you specified are string literals. One alternative:

    const char *morse_code[36] = { ".-","-...","-.-.","-..",  
    ".","..-.","--.","....","..",".---","-.-",".-..","--",  
    "-.","---",".--.","--.-",".-.","...","-","..-","...-",  
    ".--","-..-","-.--","--..","-----",".----","..---",  
    "...--","....-",".....","-....","--...","---..","----." };  
    

    a5.cpp:82:10: error: invalid operands to binary expression
    ('char [255]' and 'int')

    output += ' ';

    The variable "output" is defined as an array of chars. So you
    can't use the += operator as that only works with std::string.

    You need to review how to build C-style strings (nul-terminated).

    With suitable care when defining/declaring the char array which
    is to hold the string being built - including presetting it to
    binary zeros and ensuring that it is large enough - you can
    make use of C runtime library functions such as:

    strncat
    https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strncat-strncat-l-wcsncat-wcsncat-l-mbsncat-mbsncat-l?view=msvc-170

    strcat
    https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcat-wcscat-mbscat?f1url=%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(STRING%252Fstrcat);k(strcat);k(DevLang-C%252B%252B);k(TargetOS-Windows)%26rd%3Dtrue&view=msvc-170

    strcpy
    https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-wcscpy-mbscpy?view=msvc-170

    memcpy
    https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/memcpy-wmemcpy?view=msvc-170

    • Wayne
    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 74,936 Reputation points
    2025-04-09T00:45:33.9933333+00:00

    you don't define your morse code char break or word breaks. here is a simple sample with space as char break and / as word break (same as the sample javascript translator).

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    char* morse_code[] = {
        ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..",
        "--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-",
        "-.--","--..","-----",".----","..---","...--","....-",".....","-....","--...",
        "---..","----.","/"
    };
    char alpha_num[] = {
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U',
        'V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9',' '
    };
    
    char* lookupMorse(char c) {
        for (int i=0; i < sizeof(alpha_num); ++i) {
            if (alpha_num[i] == c)
                return morse_code[i];
        }
        return "/";
    }
    
    char lookupAlpha(char* string) {
        for (int i=0; i < sizeof(alpha_num); ++i) {
            if (strcmp(morse_code[i], string) == 0)
                return alpha_num[i];
        }
        return ' ';
    }
    
    char* alphaToMorse(char* alpha) {
        char* morse = "";
        for (int i=0; i < strlen(alpha); ++i) {
            char* match = lookupMorse(alpha[i]);
            char* newMorse = malloc(strlen(morse) + strlen(match) + 1);
            strcpy(newMorse,morse);
            strcat(newMorse,match);
            strcat(newMorse," ");
            morse = newMorse;
        }
        return morse;
    }
    
    char* morseToAlpha(char* morse) {
        bool done = false;
        int morseCharCount = 0;
        
        for (char* p = morse; !done; ++p) {
            if (*p == ' ' || (done = *p == 0)) ++morseCharCount;
        }
        
        char* alpha = malloc(++morseCharCount);
        char* pa = alpha;
        char morseBuff[8];
        char* endOfBuff = &morseBuff[7];
        char* pb = morseBuff;
        
        for (char* pm = morse, done = false; !done; ++pm) {
            char m = *pm;
            if (m == ' ' || (done = m == 0)) {
                *pb = 0;
                pb = morseBuff;
                *pa++ = lookupAlpha(morseBuff);
            } else {
                if (pb < endOfBuff)
                    *pb++ = m;
                else
                    *pb = 0;
            }
        }
        *pa = 0;
    
        return alpha;
    }
    
    int main(int argc, const char* argv[]) {
        printf("%s\n",alphaToMorse("HELLO WORLD"));
        printf("%s\n",morseToAlpha("--. --- --- -.. / -... -.-- . ---------"));
        return 0;
    }
    

  3. WayneAKing 4,926 Reputation points
    2025-04-09T20:35:59.7833333+00:00

    Note to the 2025 posters to this thread:

    The thread and the OP's code are from year 2022.

    I assume the OP has completed the assignment by now.

    • Wayne
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.