Code error for letter detection

Asked

Viewed 62 times

0

I’m working on a code for letter detection using a function, for a classroom exercise. The idea is that when I type a letter the function detects if it is:

  • maiscula = 1
  • minuscule = 0
  • something else = -1.

This returns 1, 0 and -1 returns to the main().

My return is always -1.

    int main()
    {
    char le;
    int x; x=0;

    printf ( " \n Programa que detecta se a letra e maiuscula e ou 
    minuscula");

    printf (" \nDigite uma letra : ");
    scanf ("%s", &le);

    detletra (l);

    if  ( x = 0)
    {
         printf (" \n A letra digitada e minuscula! "  );
    }
    else if  ( x = 1)
    {
         printf (" \n A letra digitada e maiscula! "  );
    }
    else if  ( x = -1)
    {
         printf (" \n A letra digitada não pertence ao alfabeto "  );
    }

    printf ( " \n %d", x);
    }


    void detletra ( char letra ) // função  processamento dos dados
    {
    if (letra >= 'a' &&  letra <= 'z') // Verifica se a letra é minuscula
    {
    int x;
    x = 0;
    return x; // retorna o valor para main
    }
    else if (letra >= 'A' && letra <= 'Z' ) // Verifica se a letra é maiscula
    {
    int x;
    x = 1;
    return x; // retorna o valor para main
    }
    else // Verifica se a letra não é do alfabeto
    {
    int x;
    x = -1;
    return x; // retorna o valor para main
    }

    }
  • return type of the method is as void

  • not to pass the return of the function to the variable x

  • Thanks for the return, in case I switch to int, the compiler claims the message |D: UNISSINOS language c Work 1 _v2 main. c|32|error: Conflicting types for 'detletra'| D: UNISSINOS language c Work 1 _v2 main. c|33|note: an argument type that has a default Promotion can’t match an Empty Parameter name list declaration|

1 answer

3


There are several mistakes, some more critical than others. Come on.

The function must return an integer data type as defined by the problem and not a type that does not return value.

The variable x is not getting the return value by the function, I changed it by taking this return. Maybe thought the x of a function is the same thing as x of the other function, but it’s not, so it returns something.

The comparison is made with the operator == and not the = which is the assignment operator, so you are changing the value of x inappropriately in the place you think is comparing something.

To ask for a letter you must use formatting %c and not %s.

I improved the names of the identifiers (I left the x which could have better name), they are important to give readability in the code. And I took comments that did not help at all to understand what is happening there, only said Obvious, comment is not for this. And of course, I organized the code a lot, I took a lot of stuff that wasn’t needed and I kept a pattern.

A more radical change I made was to simplify the comparison of the lyrics. The value of a comparison is already a number 0 or 1, so you can use the result to avoid part of the code. And even if you used the if as used in the first case, need not declare variable, assign a value to it and then use this value, there is no reason to use variable there, use the direct value.

Since it returns 1 for true and 0 for false and I want it to return 0 or -1 then subtract 1 from the result. But this account only occurs that no longer stayed in the if previous which returns the 1.

Setting the order of comparisons is something a programmer has to think about, it can greatly simplify the code. And know all the mechanisms of language to use properly and simply.

It looks like the code can get much smaller:

#include <stdio.h>
int detecta(char letra) {
    if (letra >= 'A' && letra <= 'Z') return 1;
    else return (letra >= 'a' && letra <= 'z') - 1;
}

int main() {
    char letra;
    printf("Programa que detecta se a letra e maiuscula e ou minuscula");
    printf("\nDigite uma letra: ");
    scanf("%c", &letra);
    int x = detecta(letra);
    if (x == 0) printf("\nA letra digitada e minuscula!");
    else if (x == 1) printf("\nA letra digitada e maiscula!");
    else if (x == -1) printf("\nA letra digitada não pertence ao alfabeto");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Understand what each part, what each character of the code does, so it organizes better, makes it more readable, and knows what is happening there, without seeming a magic.

  • Thanks Cool, I’ll take more care in the transponders and resume some concepts that I’m confusing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.