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