Thread: Morse code conversion

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    19

    Morse code conversion

    I am writing a program that will convert Morse code to alphabet and vice versa.

    Here is the code so far

    Code:
    #include <stdio.h> 
    
    int main() 
    {
    char *alpha[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] =
    {
        ".-",
        "-...",
        "-.-.",
        "-..",
        ".",
        "..-.",
        "--.",
        "....",
        "..",
        ".---",
        ".-.-",
        ".-..",
        "--",
        "-.",
        "---",
        ".--.",
        "--.-",
        ".-.",
        "...",
        "-",
        "..-",
        "...-",
        ".--",
        "-..-",
        "-.--",
        "--..",
    };
    int w;
    
    
    while( w != 3)
    {
        w=0;
        printf("Please select an option\n");
        printf("1. Encode\n");
        printf("2. Decode\n");
        printf("3. Quit\n");
        scanf("%d", w);  
            if(w==1)
                    printf("Encode\n");  //Encode function goes here
            else if(w==2)
                    printf("Decode\n");   //Decode function goes here
            else
                    printf("Wrong option selected\n");
    
    
    }
    }
    This is just a test program and I haven't finished yet. But I get an error when I run the program. It gets to where you can select among the three options. When I select a number and hit enter I get
    "(Name of File) has generated errors and will be closed by Windows. You will need to restart the program.

    An error log is being created."

    Does anyone know or see what I am doing wrong? Thanks for the help in advance.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    scanf("%d", w);
    Should be
    Code:
    scanf("%d", &w);

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    19
    LOL, Thank you very much. Good eye. I have made that mistake many times in the past. I have probably overlooked that error 20 times just on this program. I'll have to be more careful. Thanks again.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'll have to be more careful
    Or get a better compiler
    Or just make better use of the options on the one you have

    gcc -W -Wall -ansi prog.c
    Catches an awful lot of trivial problems like this
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Hmmm good tips salem.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Salem
    > I'll have to be more careful
    Or get a better compiler
    Or just make better use of the options on the one you have
    Or stop using scanf() and switch to fgets()

    I had to get that in there
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Or stop using scanf() and switch to fgets()
    Assuming the desire to use integers instead of characters for the menu selection, sscanf would still be the most common option after fgets and you would end up with the same potential problem of not passing an address. Naturally you could do something like this:
    Code:
    if ( fgets ( buff, sizeof buff, stdin ) != NULL ) {
      switch ( buff[strspn ( buff, " \t" )] ) {
      case '1': /* Blah */
      case '2': /* Blah*/
      case '3': /* Blah*/
      }
    }
    But as usual, everyone and their immediate family would complain that there is a better way.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  3. About the Morse code Converter
    By Amber_liam in forum C Programming
    Replies: 17
    Last Post: 05-29-2002, 08:35 AM
  4. Currency Conversion Program
    By kgraw21 in forum C Programming
    Replies: 5
    Last Post: 04-19-2002, 08:39 AM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM