C Morse code converter shows no error in compiler but doesn't work

Related searches

I am trying to write a simple program that takes a string input and then converts it into morse code.

When I compile the code and run it, I get no errors but it sometimes gives an output, sometimes doesn't, sometimes gives only the half of it. I can kind of understand that the algorithm that I use right now can be a bit slow to convert a big string but it does the same behaviour for even one character.

So what is wrong about my code?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
    char characters[26]= {'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'};
    char *morse[26] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--"," -.","---",".--.","--.-",".-."," ...","-","..-","...-",".--","-..-","-.--","--.."};
    char input[25];
    char *output[25];
    gets(input);

    int i = 0, k = 0; //Make all of the chars uppercase to compare with the array.
    while(i < 25) {
        input[i] = toupper(input[i]);
        while(input[i] != characters[k]){
            k++;
        }
        output[i] = morse[k]; //Assign the according morse code to the output's pointers.
        i++;
    }

    int n = 0;
    while(n < 25){
        if (output[n] != NULL){  //Print out everything except empty parts
            printf("%s", output[n]);
            n++;
        }
        else{
            break;
        }
    }
    return EXIT_SUCCESS;
}  

C++ forbids converting a `string` constant to `char*`, You initialize those pointers with string literals. But as the error explains, converting a string literal (called constant by the compiler) to char* is not allowed. Solution:� 10.17: Morse Code Converter (C++) Morse code is a code where each letter of the English alphabet, each digit, and various punctuation characters are represented by a series of dots and dashes. Table 10-8 from the textbook shows part of the code. Write a program that asks the user to enter a string, and then converts that string to Morse code.

You need to set k back to zero every time you search characters. Since you don't do this, you start your search from the position that the last search ended. If the next character is earlier in the alphabet, you won't find it and the search will read outside the array.

Also, if any of the input characters aren't letters, the search will never find it. The loop has no check for this.

A simpler method would be to first check isalpha() to make sure that the input is a letter, and then calculate input[i] - 'A' and use that as the index into morse.

When processing input you should stop when you get to the null terminator, not go all the way to i = 24.

int i = 0;
while (input[i]) {
    if (isalpha(input[i])) {
        input[i] = toupper(input[i]);
        output[i] = morse[input[i] - 'A'];
    } else {
        output[i] = NULL;
    }
    i++;
}

how do I morse code converting program, I'm trying to write a program that takes an input txt file, reads a line (either in english No. In the code we're showing you, it's checking to see if it's all morse code. Edit: Changed isMorse to an int, incase your compiler doesn't understand bool. i think the logic is ok, I don't see why it's giving me this error. Morse Code Converter Design a program that asks the user to enter a string and then converts that string to Morse code. Morse code is a code where each letter of the English alphabet, each digit, and various punctuation characters are represented by a series of dots and dashes. Table 8-7 shows part of the code

Use fgets to get input of variable length (just give it a character pointer) Use strlen to get the length input to control your loop. You might also want to reset the value of k when i is incremented so that you don't run out of bounds in the characters array.

Converting english to morse code - C++ Forum, and finally my teacher doesn't want me to use string toMorse (string, string[]); reply and for the references the first one worked the words are no longer but i just get a ton of errors if anyone could help with this by showing me http:// stackoverflow.com/questions/42176678/c-convert-ascii-to-morse-code. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Barmar's comment above is correct. In addition to that, your program depends on the user entering a string that consists entirely of characters. What do you think will happen in your inner while loop if your string contains, say, a "1"?

Morse code, As the standard Morse code does not contain all possible characters, you 8 Befunge; 9 C; 10 C#; 11 C++; 12 Clojure; 13 CoffeeScript; 14 D cannot play sounds by itself, so here we just translate text to dits and func note(on bool) error { data MSym = Dot | Dash | SGap | CGap | WGap deriving (Show) the errors and/or warnings my compiler says is that, for char morse[26], it all shows a multi character constant warning, no matching function for call to 'get line' and member reference base type 'char' is not a structure or union. I'm pretty sure my code right now doesn't do the job but I just want to debug it at the moment. Thank you.

Assignment 5, Learn how to read data from files in C. Practice writing simple Makefiles. In this assignment, you will build programs to convert between ascii and Morse code. In Morse code, letters are represented by various sequences of dots and dashes. If the user does not provide a filename, prints an error message and exits. Here’s a Simple Program to convert String(Text) to Morse Code in C Programming Language. Morse code is a method of transmitting text information as a series

Here is a list of best free text to Morse code converter software for Windows. Through these software, you can easily convert text into Morse code symbols (dot and dash). As you know, Morse code is one of the earliest long distance communication methods which uses dots and dashes to represent characters, numbers, etc. Earlier, only experienced persons could create and translate these cod

Send the message above in Morse code by email, Facebook or any other network by sharing a link (here is an example). Sound and/or light and speed will be used as selected here. If they know Morse code you can hide the text.

Comments
  • Where do you initialize the unused elements of output to NULL?
  • The fact it compiles only means it is correct as far as syntax. It has nothing to do with whether the code is correct logically. Use a debugger to step through the code to see how its working differently than you intended.
  • Don't use gets(), it has no bounds checking and has been removed from the language. Use fgets(stdin, input).
  • You should stop processing input when you get to the null terminator. If the user enters less than 25 characters you'll process uninitalized elements.
  • @Barmar Thanks for the previous comments, how can I stop processing the input part like you said? By using some kind of a function that gives the length of the input?
  • Thanks for the answer. Could you please explain how the - 'A' works also?
  • @Pearlescent I suppose you mean about morse[c - 'A']. Because of the check isalpha(input[i]) then char c = toupper(input[i]); it is sure c is a letter from 'A' to 'Z', so c - 'A' value between 0 and 25 and can be used as the index in morse. BTW I just see I forget to remove characters, I edit, and also add some const
  • I was thinking about fixing those problems later on. I am trying to produce the code that works firstly right now.
  • So, are you saying that the program fails when you give it exactly 25 alpha characters as input?