Error in the use of else [closed]

0

The program is giving an error when compiling.

  

else without a previous if

Something about else , is there something wrong with the condition if and else ?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char est_civil;

    printf("Digite seu estado civil: ");
    est_civil = getchar();

    if (est_civil == 'C' || est_civil == 'c');
        printf("Casado");
    else

        if (est_civil == 'S' || est_civil == 's');
            printf("Solteiro");
        else

    return 0;
}
    
asked by anonymous 11.04.2017 / 17:54

2 answers

3
  • % s should not have a semicolon at the end

  • The second if is empty. It needs to be removed.

  • The code can still be simplified, but the syntax errors are these.

    The only thing I think it's important to change is to use else instead of creating a block else if with else inside. This raises the readability of the code somewhat, so it might be interesting to see this publication * .

    Something like:

    if (est_civil == 'C' || est_civil == 'c')
        printf("Casado");
    else if (est_civil == 'S' || est_civil == 's')
        printf("Solteiro"); 
    

    Corrected code.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char est_civil;
    
        printf("Digite seu estado civil: ");
        est_civil = getchar();
    
        if (est_civil == 'C' || est_civil == 'c')
            printf("Casado");
        else if (est_civil == 'S' || est_civil == 's')
            printf("Solteiro");
    
        return 0;
    }
    

    * How to write a readable and easy-to-maintain code?

        
    11.04.2017 / 17:59
    2

    This works, and much simpler, does not it?

    #include <stdio.h>
    
    int main() {
        printf("Digite seu estado civil: ");
        char est_civil = getchar();
        if (est_civil == 'C' || est_civil == 'c') {
            printf("Casado");
        } else if (est_civil == 'S' || est_civil == 's') {
            printf("Solteiro");
        }
    }
    

    See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    Never forget to put keys in if , unless you know what you are doing, which depends on a lot of experience. Especially do not put the ; at the end of the if condition because you are closing it and nothing else will be executed as part of your block, it becomes innocuous except for side effect, which is advanced to its stage, and almost no one uses it even when it can make some sense.

    The if is a block of commands. Blocks must be enclosed in brackets. Even if eventually they can be omitted, and there are cases that can, so the compiler does not prevent, you should not do this to avoid unexpected errors. Without the keys you will be taken to dangling else .

    When you have nothing to do with else , do not use it. When what you have to do next to a else is not a if , make a else if and create a single block.

    See more in What happens if I do not specify the {}? .

        
    11.04.2017 / 18:00