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 {}? .