Thread: Homework- Morse Code output problem

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    Here is the new code. Much easier to do after after reading a few chapters ahead. A lot of the stuff in this code was mentioned in class but not defined or shown which is why I didn't use it. My problem now is the while loop not ending at the null character. Any help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    
    
    #define STR 1000
    
    
    
    
    void English_to_Morse()
    {
       int len, a, b;
    
       char phr[STR];
    
       char alpha[]="abcdefghijklmnopqrstuvwxyz1234567890";
       char *morse[37]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
                       "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
                       "..-","...-",".--","-..-","-.--","--..",".----","..---",
                       "...--","....-",".....","-....","--...","---..","----.",
                       "-----","   "};
       printf("\n\tEnter in a phrase in English for encrypting\n"
              "\tor enter 'ctrl+z' to quit\n");                 
    
    
       while(*phr!='\0')
       {                
          scanf("%s", phr);
          len=strlen(phr);
    
          for(a=0; a<len; a++)
          {
                for(b=0; b<37; b++)
                { 
    
                  if(tolower(phr[a])==alpha[b])
                     printf("%s ", morse[b]);
                }
          } 
       }              
    }
    
    
    void Morse_to_English()
    {  
       int len, a, b, c;
    
       char phr[STR];
    
       char alpha[]="abcdefghijklmnopqrstuvwxyz1234567890";
       char *morse[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
                       "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
                       "..-","...-",".--","-..-","-.--","--..",".----","..---",
                       "...--","....-",".....","-....","--...","---..","----.",
                       "-----"};
        printf("\n\tEnter a line of Morse Code for decrypting\n\n");
    
    
    
    
        while(*phr!='\0')
        { //I struggled with getting the input to read all the characters before 
          //the space as 1 index so I came up with a for loop.  It is supposed to
          //read to the space and copy the contents of that string in the 
          //array index, but it didnt work. However, just like my other function, it will not read the null
          // character and exit the loop.    
          scanf("%s", phr);
          for(a=0; a<36; a++)
          {
             if(strcmp(phr, morse[a])==0)
                printf("%c", alpha[a]);
          }                     
    
    
        }
    }
    
    
    int main()
    {  
       char ch;
    
       printf("\n\tSelect from the menu:\n"
              "\tA. English to Morse\n"
              "\tB. Morse to English\n");
       scanf("%c", &ch);
    
    
       switch(tolower(ch))
       {
          case 'a':
             English_to_Morse();
             break;
          case 'b':   
             Morse_to_English();
             break;
          default:
             break;   
       }   
    
    
    
       return 0;
    
    }   
    
    
    I wrote my E_to_M function before figuring out the strcmp(), I know there is some cleaning up to do and I wil do that, right now my concern is the loop ending.

  2. #17
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Why not change that to an indexed array?
    Code:
    while(*phr!='\0')
    to
    Code:
    while(phr[i]!='\0')// somewhere at the bottom of the while loop you will need to increment i
    Right now *phr is never changing.

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    In addition to camel-man:

    1) You don't initialize "phr" in both your functions. If "*phr" points by chance to '\0' your while-loop won't execute. I think you want a do-while-loop.

    2) You have a buffer overflow in English_to_Morse():
    Code:
    char alpha[]="abcdefghijklmnopqrstuvwxyz1234567890";
     ...
        for(b=0; b<37; b++)
        { 
            if(tolower(phr[a])==alpha[b])
    "alpha" has only 36 elements but "b" will get up to 36 which is not a valid index for "alpha".

    Bye, Andreas
    Last edited by AndiPersti; 11-30-2012 at 03:31 AM. Reason: posted unfinished answer

  4. #19
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    @camel-man I dont understand why you say *phr is never changing. scanf is reading in the string that is going to phr. scanf reads to the space, converts that piece, gets looped back through reads to the next space and so on until it reaches the null character. My book says that scanf reads the null character. phr has to change every time it gets looped through because the output does exactly what it should be doing. I will try the array form though and see if that works.

  5. #20
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    I think you are getting fscanf mixed up with scanf. Right now you just keep reading into the start of the array each time through the loop. phr is the address of the first element, no where in your code right now is scanf reading anything other that phr's first address.

  6. #21
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    My book says scanf will read in a string to a space and will continue from that space if it is looped. If you compile the program that is what it does. I can write a full sentence in the program and it will get translated properly. It would appear scanf is doing just what the book says all the way up to the null char.

    Did you compile it and try to translate something? I thought fscanf is used for file reading like fprintf.

    As an example in class our teacher wrote a program with scanf in two places. She wrote a sentence into it and had the program print out the sentence. Scanf read in the first word up to the space which was then printed to the screen and the next scanf read in the next word from the sentence and printed it, even though she wrote them both with the first scanf. That is what is happening here.

  7. #22
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    It was like this.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        char x[50];
        
        printf("enter a sentence\n");
        while(*x!='\0')
        {
           scanf("%s", x);
           printf("\n%s", x);
        }      
    
    
        
        return 0;
    
    
    }
    But again this program does not reach the null character, it does however print out every word you enter in to the string. If scanf did what you were saying then it would be printing out the first word in every sentence over and over.

  8. #23
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Phr is always pointing to the first element of the array. You tell me how it will ever exit the while loop if you do not control/increment where phr points to the next iteration through.

    In your teachers case all that is happening is scanf is going back into the buffer to scan anything left in it until reading whitespace, in which it will stop and print( then start again as long as characters are in the buffer).

    Edit: You have some false assumption that scanf changes what phr is pointing to.
    Last edited by camel-man; 12-02-2012 at 02:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Morse Code(Going the other way)
    By lilbo4231 in forum C Programming
    Replies: 21
    Last Post: 06-16-2011, 03:25 AM
  2. Problem with my morse code program
    By justin87 in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2007, 05:23 PM
  3. Morse code conversion
    By viclaster in forum C Programming
    Replies: 6
    Last Post: 12-04-2003, 09:24 PM