Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

658
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!