Thread: Please help with this program

  1. #1
    magicman66
    Guest

    Please help with this program

    This Program asks the user to input a morse code or series of morse codes and then outputs the morse code symbol and its corresponding letter. The EOF marker marks the end of the program. I got it to compile, but I'm sure there are a lot of errors that prevent me from getting this to work. Whenever I input a morse code and hit enter, it just goes to the next line and hangs there waiting. What am I doing wrong here?

    Thanks for the help.


    Code:
    /* MorseCode.c
     * -----------
     * This program converts Morse code characters
     * to their letter equivalents.
     */
    
    #include <stdio.h>  
    #include <string.h> 
    #include <ctype.h>
    
    int search(const char *a[], const char *key, int size);
    
    
    int main()
    {
      int element;
      int size=5;	
      
      /* morse codes of the 26 letters */
      char *morse[26] = {
          ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
          ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
          "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
      };
    	
      char *letters[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 *next[5];
      int v;
    
      printf("Enter Morse codes separated by spaces (type EOF to quit)\n\n");
    
      /* sentinel based loop -- the end of file marker is the sentinel */
      while (scanf("%s", next) != EOF) 
      {
          ++v;
    	  element = search(morse, next[5], size);
    	  if (element != -1)
    	  {
    		printf("%s %c\n", morse[v], letters[v]);
    	  }
      }   
      return 0;
    }
    
    int search(const char *a[], const char *key, int size)
    {
    	int n;
    
    	for (n=0; n<=size-1 ; ++n)
    	{
    		strcmp(*a, key);
    		if (strcmp(*a,key) ==0)
    			return 0;
    	}
    	
    	return -1;
    }/* MorseCode.c
     * -----------
     * This program converts Morse code characters
     * to their letter equivalents.
     */
    
    #include <stdio.h>  
    #include <string.h> 
    #include <ctype.h>
    
    int search(const char *a[], const char *key, int size);
    
    
    int main()
    {
      int element;
      int size=5;	
      
      /* morse codes of the 26 letters */
      char *morse[26] = {
          ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
          ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
          "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
      };
    	
      char *letters[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 *next[5];
      int v;
    
      printf("Enter Morse codes separated by spaces (type EOF to quit)\n\n");
    
      /* sentinel based loop -- the end of file marker is the sentinel */
      while (scanf("%s", next) != EOF) 
      {
          ++v;
    	  element = search(morse, next[5], size);
    	  if (element != -1)
    	  {
    		printf("%s %c\n", morse[v], letters[v]);
    	  }
      }   
      return 0;
    }
    
    int search(const char *a[], const char *key, int size)
    {
    	int n;
    
    	for (n=0; n<=size-1 ; ++n)
    	{
    		strcmp(*a, key);
    		if (strcmp(*a,key) ==0)
    			return 0;
    	}
    	
    	return -1;
    }

    sample output

    Enter Morse codes separated by spaces (type EOF to quit).
    -.-.
    -.-. C
    .--. .-. --- --. .-. .- -- -- .. -. --.
    .--. P
    .-. R
    --- O
    --. G
    .-. R
    .- A
    -- M
    -- M
    .. I
    -. N
    --. G

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    A brief scan shows some quiet errors
    Code:
    while (scanf("%s", next) != EOF)
    I do not think this will work. The return value of scanf() is actually the number of bytes stored, not the value inputted, as you seem to think? You will need to compare next to EOF in some other fashion, maybe outside the while(...) structure.

    Code:
    char *morse[26]
    I've never seen an array declared like this. You can use either an array or a pointer, not sure if you can use both at the same time. it looks like you are declaring an array of pointers to characters, which is not exactly like you want. Try const char* morse="stuff"; or const char morse[26]={"stuff"};

    Hope this helps a little.
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The return value of scanf() is actually the number of bytes stored
    The return value is the number of conversions and assignments (ie, 1 if all is well), or EOF when at end of file (ctrl-z or ctrl-d usually)
    So it seems OK so far...

    > int size=5;
    The way you use this suggests that it should be
    int size=26;

    > char *next[5];
    Should be
    char next[5];

    > element = search(morse, next[5], size);
    Try
    element = search(morse, next, size);

    > printf("%s %c\n", morse[v], letters[v]);
    printf("%s %s\n", morse[v], letters[v]);


    Though perhaps later, you will want
    printf("%s %c\n", morse[element], letters[element ]);
    And you may want search to return the value of 'n' rather than 0 when a match is found

  4. #4
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    >char *morse[26]
    >I've never seen an array declared like this.

    What???

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >char *morse[26]
    >I've never seen an array declared like this.
    You mean like this?:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char *stuff[2] = {"line 1", "line 2"};
    	
    	printf("%s\n%s\n", stuff[0], stuff[1]);
    	
    	return (0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    >You mean like this?:

    Yea, I thought it was common to use indirect addressing to save space.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM