Click here to Skip to main content
15,888,100 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So basically I am making a code that encodes a letter, just for fun. This code has caused some major issues, I can't fix it. Help please?

C
#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 always have this issue, coding brackets do not like me.
Posted
Updated 22-Feb-21 20:09pm
v2

You have opening brackets for the "main" and "switch" lines, but only one closing bracket for "main".

To make things a bit easier to see, put the brackets on their own lines, not at the end of lines:
C#
#include "stdio.h"

int main(void)
{
    printf("This code will make a morse code. Enter lowercase letters");
    switch
    {
        case 'a':
            return ".-";
        ...
        default:
         printf("Found wrong characters");
        exit(0);
    }  <-- missing closing bracket
  return 0;
}

You're going to run into other issues with this code, but let's see if you can figure those out.
 
Share this answer
 
You can use tolower and that will allow the user to enter either case of letter. You could also use an array indexing scheme if you want to. The goal would be to avoid using a switch-case statement. Here's one way you can have a function that returns the Morse code string for a given character using two arrays. The arrays are sorted according to the alphabetic and numeric orders of the characters so that indexes can be used to access them.
C++
typedef const char * PCSTR;

// function returns Morse code string for a given character
// or zero if character is not valid

PCSTR GetMorseCodeString( int character )
{
    static PCSTR letterStrings[] =
    {
          ".-"    // 'a'
        , "-..."  // 'b'
        , "-.-."  // 'c'
          .................
        , "--.."  // 'z'
    };
    
    static PCSTR numberStrings[] =
    {
          "-----"  // '0'
        , ".----"  // '1'
        , "..---"  // '2'
          .................
        , "----."  // '9'
    };

    int lowerCase;
    int index;
    if( character >= '0' ) && ( character <= '9' ) )
    {
        index = character - '0';       // get the numeric value
        return numberStrings[ index ];
    }

    lowerCase = tolower( character );
    if( character >= 'a' ) && ( character <= 'z' ) )
    {
        index = character - 'a';       // 'a' is index 0
        return letterStrings[ index ];
    }
    return nullptr;                    // character was not valid
}
Then you need to prompt the user for an input string and translate each character of the string into its Morse code equivalent by calling this function. Alternatively, you can read a file of text strings and translate each one the same way.
 
Share this answer
 
Comments
CPallini 23-Feb-21 2:09am    
5.Note: Unfortunately there is no nullptr in C.

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