0

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.

Clay
  • 747
  • 7
  • 12
  • 1
    It would be useful to know what the logic error is...? – EdChum Dec 03 '18 at 14:49
  • 1
    `break;` after `return`. :-) Didn't you get any warning about this? – Scheff's Cat Dec 03 '18 at 14:49
  • 2
    You are returning `0` in the default case, i.e. a null-pointer. If you put out something like that, what happens? A zero? String termination? Magic? Try printing that out and re-state what happens. – Thomas Lang Dec 03 '18 at 14:49
  • 2
    Debugger is helping in such a cases. – Eugene Sh. Dec 03 '18 at 14:49
  • 1
    On top of all that you're passing the pointer to the string (`char*`) to a function that takes a `char`. I think what you intended was looping through the string, calling the function on very `char` in it. – Blaze Dec 03 '18 at 14:51
  • I don't understand. Your question is about C language but you include the C++ tag. Which language are you using? Please edit the tags accordingly. BTW, C and C++ are two distinct languages. – Thomas Matthews Dec 03 '18 at 14:51
  • @ThomasMatthews I saw this coming and removed [tag:c++] - but not fast enough... – Scheff's Cat Dec 03 '18 at 14:52
  • `printf(morseEncode(p[i]));` isn't the right way to do things; either should be `puts(morseEncode(p[i]));` or `printf("%s",morseEncode(p[i]));` – Chris Turner Dec 03 '18 at 14:52
  • You may want to go the array lookup route, where `string = array[letter];` – Thomas Matthews Dec 03 '18 at 14:54
  • I kown that inside main function I declare "phrase" as const char and then try to change it with scanf but when I change it to char I get conversion errors and can't fix it. – Clay Dec 03 '18 at 14:56
  • @ThomasMatthews I added tag to get attention. – Clay Dec 03 '18 at 14:58
  • @Scheff No I didn't. – Clay Dec 03 '18 at 15:00
  • _I added tag to get attention._ Yeah, this was clear to me. It's called "tag spamming" and considered as bad behavior. Please, read the [Tour](https://stackoverflow.com/tour) to get a short intro to SO. I see you did not yet. – Scheff's Cat Dec 03 '18 at 15:03
  • Clay, a much better process is to use a debugger. Debugging your program is a lot faster than Correctly posting to StackOverflow and waiting for somebody to inspect or debug your program for you. – Thomas Matthews Dec 03 '18 at 15:04
  • @ThomasLang I saw it but forgot to fix it. Thanks, but it is not the main problem I think. – Clay Dec 03 '18 at 15:04
  • Concerning the `break`s after `return`s: If the compiler detects unreachable code (and the `break`s after `return`s are unreachable), it might print a warning. In general, warnings are interesting as things might go wrong although they are syntactically correct. If there is no warning than the compiler ignores this specific case, or the warning is simply suppressed/not enabled. I would tend to the latter (but I wouldn't bet). – Scheff's Cat Dec 03 '18 at 15:10
  • @ThomasMatthews I used and it gave me some errors the first time i wrote. Then I fixed the errors and compiled it again and it executed the program, no warnings but I couldn't find my mistake. that's why im here. – Clay Dec 03 '18 at 15:21
  • @Scheff After deleting breaks it worked but only for first alphabet of the string. I think another of my mistake is inside the for loop. Thank you btw. – Clay Dec 03 '18 at 15:29
  • @ChrisTurner wow, its a foolish mistake that i missed. thx – Clay Dec 03 '18 at 15:30
  • Unreachable code should be simply effect-less (except it may influence [Undefined Behavior](https://stackoverflow.com/a/4105123/1505939) what doesn't make it better). – Scheff's Cat Dec 03 '18 at 15:38

2 Answers2

4
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.

Chris Turner
  • 8,082
  • 1
  • 14
  • 18
  • `scanf("%s",phrase);` is like `gets(phrase);` Suggest input limitation like `scanf("%99s",phrase);` for a learner. – chux - Reinstate Monica Dec 03 '18 at 14:56
  • @chux not entirely like `gets` as it'll only get one word which I'd hope would be less than 99 characters...I expect since the code asks for a phrase they should be using `fgets` though – Chris Turner Dec 03 '18 at 15:04
0

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);
Clay
  • 747
  • 7
  • 12