0

I mainly want my else statement to work, but it's not working.

int main(int argc, string argv[])
{


if (argc != 2)
{
    printf("Usage: ./caesar key\n"); 
}

else if (argc == 2)

{
    for (int i = 0; i < strlen(argv[1]); i++)
    {

        if (isdigit(argv[1][i]) == 0)
        {
            printf("Usage: ./caesar key\n");
        }

    }

else 
{
    printf("Sucess\n");
}

}



}
2
  • The else if block is missing a closing } May 13 '20 at 15:21
  • It's there, just in the wrong place.
    – Cliff B
    May 13 '20 at 18:16
1

First of all, this is why it's important to get the indentation aligned correctly so you can see what goes with what. Here's the corrected indentation.

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n"); 
    }
    else if (argc == 2)
    {
        for (int i = 0; i < strlen(argv[1]); i++)
        {
            if (isdigit(argv[1][i]) == 0)
            {
                printf("Usage: ./caesar key\n");
            }
        }
        else 
        {
        printf("Sucess\n");
        }
    }  // else clause should follow this line

}

The problem is that the else statement is not paired to any if or else if statement, because of the layout of the curly braces. You would need to move the else or a curly brace to get it properly paired. If you were to move the else clause below the curly brace that I marked, it would work.

Having said that, this whole setup is not a best practice. Here's why. First of all, there should be return statements following each printf (error message). Once that's done, all of the elseif and else statements become unnecessary.

The idea is NOT to surround the entire code that follows (which is where you're heading, and a common error by new programmers.) The reason not to do so is that it leads to confusion when the code is inevitably modified later. Someone, or even you, may not be familiar with the code and could miss that there's a closing curly brace at the end of the program that's connected to an else near the beginning.

Instead, the best practice is to do the tests for correct input, but contain those tests in a few lines of code, with no dependencies later. To explain with an example, you'd want to do this:

if (argc != 2)
{
    printf("Usage: ./caesar key\n"); 
    return 1;
}

for (int i = 0; i < strlen(argv[1]); i++)
{
    if (isdigit(argv[1][i]) == 0)
    {
        printf("Usage: ./caesar key\n");
        return 2;
    }
}

printf("Sucess\n"); 
// the rest of the code can follow with no dependence on the earlier tests,
// and no closing curly brace at the end. The code above is decoupled from all that follows.

If you study this code, you'll note a couple things. Each time something is encountered that results in printing an error message, it's followed by a return that terminates the program immediately. Because of the return statements, the code that follows will never run if the prior error is detected. That eliminates the need for an else or if else construct. It also decouples the following code from the test conditions.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • So yes the second code you posted is the right thing, I knew that already. But it still annoyed me that my else statement was not working. After all the logic was correct. For your first answer I'll retry it, but I submitted the code yesterday.
    – Dave
    May 14 '20 at 21:47
  • Once you fix your code, you're allowed to resubmit. submit50 will take the highest score. Suggest you run the source code through style50 first though.
    – Cliff B
    May 14 '20 at 21:50
  • I did the proper code yesterday, without the else clause. Wish I could post it. Ran it through style50 and check50 gave me 100.
    – Dave
    May 14 '20 at 21:56
  • because of the positioning of all the curly braces, there's no if statement for the else clause to pair with. If you really want an explanation, send me a direct message over on Stack Exchange and I'll discuss it in private.
    – Cliff B
    May 14 '20 at 22:02
0

Is the position of "else" correct?

if {} else if{ for{ if{} } else { } } ?

Respectively, the "else"-statement is expecting a corresponding "if", I guess

0

what your third else is opposed to?

You must log in to answer this question.

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