So it works, and much simpler, no?
#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");
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Never stop putting keys on if
, unless you know what you’re doing, which depends on a lot of experience. Especially don’t put the ;
at the end of the if
because it is closing it and nothing else will run as part of its block, it becomes innocuous except by side effect, which is advanced to its stage, and almost no one uses it even when it can make any sense.
The if
is a command block. Blocks should be placed between keys. Even though they may eventually be omitted, and there are cases that can, so the compiler does not prevent it, it should not do this to avoid unexpected errors. Without the keys you incur dangling Else.
When you’ve got nothing to do in one else
, do not use it. When what you have to do right after a else
but a if
, make a else if
and create a single block.
See more in What happens if I don’t specify the { }?.
I got it, I went soft because I put point and comma in the if and Else condition, and there is no haha. Now I ran the program.
– Rodrigo Muniz
From what you’re saying, it seems to be the case mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.
– Jéf Bueno
Voce knows that "use Else if" and "create an Else block with an if inside" are exact the same thing, it’s not?
– jsbueno
Yes, that’s why I said I would change. Why? That’s not clear?
– Jéf Bueno