Click here to Skip to main content
14,865,088 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How do I complete this code and/or fix it?

#include "stdio.h"
int main(void) {
printf("This code will make a morse code. Enter lowercase letters");
    switch {
    case 'a':
        return ".-";
    case 'b':
        return "-...";
    case 'c':
        return "-.-.";
    case 'd':
        return "-..";
    case 'e':
        return ".";
    case 'f':
        return "..-.";
    case 'g':
        return "--.";
    case 'h':
        return "....";
    case 'i':
        return "..";
    case 'j':
        return ".---";
    case 'k':
        return "-.-";
    case 'l':
        return ".-..";
    case 'm':
        return "--";
    case 'n':
        return "-.";
    case 'o':
        return "---";
    case 'p':
        return ".--.";
    case 'q':
        return "--.-";
    case 'r':
        return ".-.";
    case 's':
        return "...";
    case 't':
        return "-";
    case 'u':
        return "..-";
    case 'v':
        return "...-";
    case 'w':
        return ".--";
    case 'x':
        return "-..-";
    case 'y':
        return "-.--";
    case 'z':
        return "--..";
    case '1':
        return ".----";
    case '2':
        return "..---";
    case '3':
        return "...--";
    case '4':
        return "....-";
    case '5':
        return ".....";
    case '6':
        return "-....";
    case '7':
        return "--...";
    case '8':
        return "---..";
    case '9':
        return "----.";
    case '0':
        return "-----";
        
    default:
         printf("Found wrong characters");
        exit(0);
    }
  return 0;
}


What I have tried:

I have tried a switch function.
Posted
Updated 2-Mar-21 22:32pm

Apparently you didn't take my advice last time. That's OK, I will revise it. You have the basis for a function so you should write one. Here's how it could look :
C++
const char * GetMorseCodeString( int chr )
{
    int chrin = tolower( chr );    // force character to be lower case
    switch( chrin )
    {
    case 'a':
        return ".-";
    case 'b':
        return "-...";
    case 'c':
        return "-.-.";
    case 'd':
        return "-..";
    case 'e':
        return ".";
    case 'f':
        return "..-.";
    case 'g':
        return "--.";
    case 'h':
        return "....";
    case 'i':
        return "..";
    case 'j':
        return ".---";
    case 'k':
        return "-.-";
    case 'l':
        return ".-..";
    case 'm':
        return "--";
    case 'n':
        return "-.";
    case 'o':
        return "---";
    case 'p':
        return ".--.";
    case 'q':
        return "--.-";
    case 'r':
        return ".-.";
    case 's':
        return "...";
    case 't':
        return "-";
    case 'u':
        return "..-";
    case 'v':
        return "...-";
    case 'w':
        return ".--";
    case 'x':
        return "-..-";
    case 'y':
        return "-.--";
    case 'z':
        return "--..";
    case '1':
        return ".----";
    case '2':
        return "..---";
    case '3':
        return "...--";
    case '4':
        return "....-";
    case '5':
        return ".....";
    case '6':
        return "-....";
    case '7':
        return "--...";
    case '8':
        return "---..";
    case '9':
        return "----.";
    case '0':
        return "-----";
    }
    return NULL;    // unrecognized
}
Now all you have to do is call that to get a the code strings for various characters that could come from user input or be read from a file or where ever else you want to obtain data from.

Note the use of the tolower function. That will allow you to accept upper case characters also and you don't have to care which it is.
   
v3
Comments
[no name] 3-Mar-21 8:02am
   
Thank you. I did take your advice.
Get rid of that awful switch block and use a lookup table:
C++
char* letters[] = { ".-", "-...", "-.-.", "-..", ".",
                    "..-.", "--.", "....", "..", ".---",
                    "-.-", ".-..", "--", "-.", "---",
                    ".--.", "--.-", ".-.", "...", "-",
                    "..-", "...-", ".--", "-..-", "-.--", "--.." };
index = chrin - 'a';
return letters[index];

You just need to add the code for numbers, and a check for an illegal character.
   
Comments
[no name] 3-Mar-21 8:04am
   
Thank you
Rick York 3-Mar-21 11:42am
   
That was my advice last time too. A simple array of strings would work pretty well. A switch statement can be optimized pretty well with those cases but it is much uglier than it needs to be.
Richard MacCutchan 3-Mar-21 11:53am
   
I missed your solution to the other question. Oh well, nice to know we think alike.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900