Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

659
Vistas
Error: add explicit braces to avoid dangling else. C

I'm using gedit and my complier is clang. I've been getting a couple of these errors recently and not sure how to fix (error in title and referring to the else statement).

 if(isupper(ptext[i]))
            if ((((ptext[i]+k)%26)+52) < 65 || (((ptext[i]+k)%26)+52) > 90)
            {
                printf("%c", (((ptext[i]+k)%26)+78));
            }
            else
            {
                printf("%c", (((ptext[i]+k)%26)+52));
            }

What should I add/remove/fix? Thanks in advance :)

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Your outer if is missing the braces:

if(isupper(ptext[i]))
{
        if ((((ptext[i]+k)%26)+52) < 65 || (((ptext[i]+k)%26)+52) > 90)
        {
            printf("%c", (((ptext[i]+k)%26)+78));
        }
        else
        {
            printf("%c", (((ptext[i]+k)%26)+52));
        }
}

Personally, I would extract some of the common elements into variables:

char something1 = ptext[i];
if(isupper(something1))
{
    char something2 = (something1+k)%26;
    if ((something2+52) < 65 || (something2+52) > 90)
    {
        printf("%c", (something2+78));
    }
    else
    {
        printf("%c", (something2+52));
    }
}

And maybe even put a char something3 = something2 + 52; in there too. Of course, with more meaningful variable names.

over 4 years ago · Santiago Trujillo Denunciar

0

C is not like Python where it tells what belongs to what by indentation. Because in C, all white space is ignored. You need to use braces to tell the compiler which if statement the else belongs to.

The C compiler doesn't know, so it's asking you to specify:

Is it this?

 if(isupper(ptext[i]))
 {
            if ((((ptext[i]+k)%26)+52) < 65 || (((ptext[i]+k)%26)+52) > 90)
            {
                printf("%c", (((ptext[i]+k)%26)+78));
            }
            else
            {
                printf("%c", (((ptext[i]+k)%26)+52));
            }
 }

or this?

 if(isupper(ptext[i]))
 {
            if ((((ptext[i]+k)%26)+52) < 65 || (((ptext[i]+k)%26)+52) > 90)
            {
                printf("%c", (((ptext[i]+k)%26)+78));
            }
 }
 else
 {
            printf("%c", (((ptext[i]+k)%26)+52));
 }

This is an example of a "dangling" else.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda