Leidolf
Leidolf

Reputation: 837

A morseEncoder program in C but have a logic error

I have written a "Morse Encoder" program in C. I can execute the code, there are no errors but a logic error. It does not give what I want. User types something as a string then hits enter but output does not happen.

Here is my code. Have a look at it. Maybe you notice what I did wrong.

#include <stdio.h>
#include <string.h>
#define SIZE 100

const char* morseEncode(char x){
    switch(x){
        case 'A':
        case 'a':
            return ".-";
        case 'B':
        case 'b':
            return "-...";
        case 'C':
        case 'c':
        case 'Ç':
        case 'ç':
            return "-.-.";
        case 'D':
        case 'd':
            return "-..";
        case 'E':
        case 'e':
            return ".";
        case 'F':
        case 'f':
            return "..-.";
        case 'G':
        case 'g':
        case 'Ğ':
        case 'ğ':
            return "--.";
        case 'H':
        case 'h':
            return "....";
        case 'I':
        case 'ı':
        case 'İ':
        case 'i':
            return "..";
        case 'J':
        case 'j':
            return ".---";
        case 'K':
        case 'k':
            return "-.-";
        case 'L':
        case 'l':
            return ".-..";
        case 'M':
        case 'm':
            return "--";
        case 'N':
        case 'n':
            return "-.";
        case 'O':
        case 'o':
            return "---";
        case 'Ö':
        case 'ö':
            return "---.";
        case 'P':
        case 'p':
            return ".--.";
        case 'Q':
        case 'q':
            return "--.-";
        case 'R':
        case 'r':
            return ".-.";
        case 'S':
        case 's':
        case 'Ş':
        case 'ş':
            return "...";
        case 'T':
        case 't':
            return "-";
        case 'U':
        case 'u':
            return "..-";
        case 'Ü':
        case 'ü':
            return "..--";
        case 'V':
        case 'v':
            return "...-";
        case 'W':
        case 'w':
            return ".--";
        case 'X':
        case 'x':
            return "-..-";
        case 'Y':
        case 'y':
            return "-.--";
        case 'Z':
        case 'z':
            return "--..";
        default:
            return NULL;
    }
}

void morseCode (const char *p){
    for(int i=0;p[i];i++){
        printf("%s/",morseEncode(p[i]));
    }
}

int main() {
    char phrase[SIZE];
    printf("Code is non-sensitive to letters.\nEnter phrase: ");
    scanf("%c",phrase);
    puts("");
    morseCode(phrase);
}

I think I did something wrong in conversions such as const char* to char or vice-versa.

Upvotes: 0

Views: 88

Answers (2)

Leidolf
Leidolf

Reputation: 837

The problem is that I ask a character as an input.

scanf("%c",phrase);

So, eventhough user inputs a string, it only saves the first character. Changing it to ask for a string solved the logical error.

scanf("%s",phrase);

Upvotes: 0

Chris Turner
Chris Turner

Reputation: 8142

const char *phrase;

phrase is uninitialised, so when you do

scanf("%s",phrase);

you're writing into some random place in memory causing undefined behaviour.

You need to allocate space for your string first. Either as an array

char phrase[100];

or by allocating memory

char *phrase=malloc(100);

And also the const qualifier in the declaration makes little sense since you change the contents when you pass it to scanf. Having it on the function is fine as the functions don't change the value.

Upvotes: 4

Related Questions