-1

I'm doing a project where I need to convert a text from a file to morse. For this I stored the alphabet and its respective code morse (which is also in a file) into a struct..

The file "original.txt" is something like:

My name is Olivia
We are in 2019
3 plus 4 is 7

the file "morse.txt" is:


    A .-*  

    B -...* 

    C -.-.*

    D -..*

    E .* //the file has the 26 letters and the 10 digits

etc..

now the only problem I have is:

warning: comparison between pointer and integer 
> if (line[i] == l[j].letters){ (line 25)`
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    /* Constants */
    #define T_MORSE 8
    #define Q_LETTERS 26
    #define Q_NUMBERS 10
    #define Q_MORSE (Q_LETTERS+Q_NUMBERS)
    #define MAX_STRING_SIZE 50



    typedef struct{          /* needs a position to the string finisher \0               */
        char letters[5];     /* stores the letters , get it as a string , but use only the first letter */
        char lettersMorse[T_MORSE];/* stores the respective morse symbols of the letters                 */
    }alphabet;
    alphabet l[Q_MORSE];

    void process(const char *line) {
        int j, i;
        for (i = 0; line[i]; ++i) {
            for ( j = 0; j < 37; ++j ){      //vector for morse letters
             if (line[i] == l[j].letters){
              printf("%s", l[j].lettersMorse);
              printf("   ");}
            }
        }
    }

    int main(){
        int cont=0,j;
        FILE *arch;
        arch = fopen("morse.txt","r");
        if( arch == NULL ){
            printf("ERROR");
            return 0;
        }
        else{
            while(fscanf(arch,"%s%s",l[cont].letters,l[cont].lettersMorse) != EOF){
                l[cont].letters[1] = '\0';                            /* eliminates unwanted characters           */
                for(j=0; j<strlen(l[cont].lettersMorse); j++){
                    if(l[cont].lettersMorse[j] == '*'){               /* if an asterisk is found                */
                       l[cont].lettersMorse[j] = '\0';                /* eliminate asterisk                   */
                       break;
                    }
                }

                cont++;                                             /*points to the next position of the vector     */
            }
        }
        fclose(arch);

        FILE *fp;
        char line[MAX_STRING_SIZE];
        fp = fopen("original.txt", "r");
        if (arch==NULL){
        printf("ERROR");}
        while (fgets(line,MAX_STRING_SIZE,fp) != NULL) {
        process(line);
        }
        fclose(fp);

        return 0;
    }

'''
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
Ognum
  • 19
  • 7
  • 2
    What content does your definition file for the morse alphabet contain? Is the content of `l` correct after reading the file? You provide space for 37 symbols. That is enough for 26 letters, 10 digits and 1 other characet. How do you handle upper/lower case letters? BTW: using hard coded magic numbers like `37` is a bad idea in most cases. You should define a macro and use it in every place where the number is needed. – Gerhardh Dec 27 '19 at 14:40
  • 5
    You might want to read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Gerhardh Dec 27 '19 at 14:49
  • The file with the letters is : A .- B -... C -.-. D -.. etc (each letter below the other) and in l[i].letters I stored the latin letters and in l[i].lettersMorse its respective morse code. Both in the struct alphabet. Thats what I thought to store them. Yesterday when I tried it was all Ok, but now it just read the letter A... – Ognum Dec 27 '19 at 15:40
  • 1
    Ognum, why does code not check the return value of `fscanf(arch, ...)`? Why does the loop with `fscanf()` allow for more than 37 iterations? – chux - Reinstate Monica Dec 27 '19 at 16:41
  • I don know... I put 37 because of the 26 letters and the 10 digits. Thats the file my professor gave to me and I cant modified. What I need to do is store the letters into a struct but I dont know why the way I put it is not working... – Ognum Dec 27 '19 at 17:10
  • @Ognum please add a few lines as example to your question. – Gerhardh Dec 28 '19 at 10:05
  • Please answer remaining questions of my first comment. – Gerhardh Dec 28 '19 at 10:07
  • regarding: `if (arch==NULL){ printf("ERROR");` 1) error messages should be output to `stderr` and when the error is from a C library function, should also output the text reason the system thinks the error occurred. The function: `perror()` is made for that purpose. In the current scenario, the call to `fopen()` failed, so must not call `fclose()` on it. Best to follow the call to `perror()` with `exit( EXIT_FAILURE );` – user3629249 Dec 28 '19 at 11:14
  • rather than reading some file to obtain the morse values, 1) code a table of pointers to char and where each pointer points, code the morse sequence for that char. Use `tolower()` from `ctype.h` so the code only has to handle lower case letters – user3629249 Dec 28 '19 at 11:16
  • @Gerhardh I made some modifications to my code. And I put an example above of what my file "morse.txt" looks like. Now I have just a warning: comparison between pointer and integer > if (line[i] == l[j].letters){ (line 25) that I dont know how to solve... – Ognum Dec 29 '19 at 13:30
  • Please do not change the content of your question to make previous comments useless. Do you **really** have empty lines in your morse file? Will you please answer my remaining questions? – Gerhardh Dec 29 '19 at 15:12

1 Answers1

0

Strictly answering the question and not doing other code review:

if (line[i] == l[j].letters)

The problem here is line is a string, so line[i] is a single character (which in C is also an integer number, hence the warning text about integer). But letters is a string, IOW an array of char, which is considered pointer to char in context like this (hence the warning text about pointer).

Reading that one comment in your code, you want first char in letters, so:

if (line[i] == l[j].letters[0])

hyde
  • 60,639
  • 21
  • 115
  • 176