2

I am working on pset2, caesar.c, and I am currently getting the error message below:

caesar2.c:32:9: error: expected expression
else if (islower(plaintext[i]));
^ 
caesar2.c:38:9: error: expected expression
else
^ 
2 errors generated.

Below is my code and I can't figure out why I'm getting the error message. I've checked all my syntax, but I don't know where to go from here. Any advice/suggestions would be most appreciated!

Thank you!

// Checks if [i] is upper case and ciphers
if (isupper(plaintext[i]));
{
    // INSERT FORMULA for upper case
}

// Checks if [i] is lower case and ciphers
else if (islower(plaintext[i]));
{
    // INSERT FORMULA for lower case
}                

// Reprints non-alpha as is
else
{
    printf("%c", plaintext[i]);
}

2 Answers 2

3

It's a syntax problem. You have an if(...); statement. The problem is that you are adding a semicolon to the end before executing the block of code in the curly braces. A semicolon is an end of statement marker, so your basically saying if(something_is_true) <do nothing> ; Next, the code in the curly braces executes every time because it is treated as just the next code block in the program.

The compiler allows this, but it's throwing an error because of the else clause that follows. An else clause must follow and be associated with an if statement. The semicolon combined with the code block is breaking the association.

Dump the two stray semicolons and this problem should disappear.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

1
  • Thank you! I removed the semicolons and it solved my problem perfectly! :)
    – Roxanne
    Commented Jun 27, 2015 at 17:23
5

@Cliff B's answer is almost correct except that it's not the semicolon that's breaking the syntax rule. it's the compound statement (the curly braces and the contents in between them).

regardless of that being useless, it would be fine if you did

if (condition);
else
{
    // do something
}

a statement that only contains a semicolon is referred to as a null statement. it's a statement that doesn't do anything. syntactically, you must have only one statement after an if, an else if, or an else (whether this statement is compound or not).

here you're having 2 statements, a null statement and a compound statement and that's what the compiler is complaining about (though the error message is confusing as usual).

see https://www.youtube.com/watch?v=WB5vMXNbYag and https://www.youtube.com/watch?v=-MUZL3kiPKI!

2
  • Thank you for your help! :)
    – Roxanne
    Commented Jun 27, 2015 at 17:23
  • Kareem is absolutely right. I'm surprised that I missed that. I guess I'm so used to seeing the ` if(...) {...} else {...}` form that I forgot that the null statement form is valid.
    – Cliff B
    Commented Jun 27, 2015 at 17:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .