Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: add explicit braces to avoid dangling else. C

Tags:

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 :)

like image 714
TeachMe Avatar asked Jul 23 '15 23:07

TeachMe


1 Answers

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.

like image 132
shoover Avatar answered Sep 19 '22 15:09

shoover