Thread: struggling with function issue

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    9

    struggling with function issue

    Hi guys. Im doing a morse code encoder and Im struggling with some things. Does any one knows why it doesnt convert my string when I compile? Do you know tell me if its ok the connection between the char function and the main one?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct {
            char letters;
            char lettersMorse[6];
        }alphabet;
        alphabet l[37];
    
    char encode (const char *s)
    {
      int i, j;
      for ( i = 0; s[i]; ++i )
      {
          for ( j = 0; j < 37; ++j )
          {
            if ( toupper(s[i]) == l[j].letters )
            {
                printf("%s", l[j].lettersMorse);
            }
          }
      }
    }
    
    int main()
    {
        int i=0;
    
        FILE *arch;
    
        arch = fopen("morse1.txt", "r");
    
        if (arch==NULL){
            printf("ERROR");
        }
        else{
            while (!feof(arch)){
                fscanf(arch, "%c %s\n", &l[i].letters, l[i].lettersMorse);
                // store the alphabet letters in 'letters' and its respective morse
                // code in 'lettersMorse' of the struct
            }
        }
        fclose(arch);
    
        char text[50];
        printf("Write your text to be encode\n");
        scanf(" %s", text);
        encode(text);
    
        return 0;
    }

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    866
    The only thing I can think of is checking that file of yours, make sure it has every letter

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Loop lines 39-43, you're not incremementing 'i' so everything is being stored in the first element of the array (the rest are uninitialized)

  4. #4
    Registered User
    Join Date
    Dec 2019
    Posts
    9
    Ohh true. My mistake. Thanks a lot

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    866
    Quote Originally Posted by Hodor View Post
    Loop lines 39-43, you're not incremementing 'i' so everything is being stored in the first element of the array (the rest are uninitialized)
    lol you got better eyes than me XD

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Quote Originally Posted by awsdert View Post
    lol you got better eyes than me XD
    That might be why I wrote "incremementing" rather than "incrementing" :-o

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struggling to implement function
    By gajya in forum C Programming
    Replies: 6
    Last Post: 12-23-2019, 09:25 AM
  2. Replies: 7
    Last Post: 09-12-2015, 01:34 AM
  3. Replies: 5
    Last Post: 03-26-2013, 02:17 PM
  4. Function issue
    By elwad in forum C Programming
    Replies: 5
    Last Post: 09-06-2009, 05:55 AM
  5. Struggling w/ string_replacement function
    By cwafavre in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 07:08 AM

Tags for this Thread