Why doesn’t my code make sense?

Asked

Viewed 67 times

-1

In my code, I have three "Cin" s that would be three questions to the user, for training of ifs. In this case, at the end we have

if((cadastro == 1) && ((ativo == 1) || (logado == 1))){
    printf("Tudo certo!\n\n");
} else {
    printf("Algo deu errado.\n\n");
}

That is, if the registration variable is a 1, and one of the other two variables on the side, is 1, I would have the answer "All Right", right?
For according to the truth table of ||, just one of the two being true, then the answer is true, right?
But by executing and giving the answer S, N and N, I get the answer "All right", someone can explain to me why?

Complete code.

#include <stdio.h>

using namespace std;
int main(){

int cadastro, ativo, logado;
char opcao;

cadastro, ativo, logado=0;



printf("Deseja cadastrar sua conta? S/N\n\n");
scanf(" %c", &opcao);

if((opcao == 'S') || (opcao == 's')){
    printf("Conta cadastrada.\n\n");
    cadastro = 1;
}

printf("Deseja ativar sua conta? S/N\n\n");
scanf(" %c", &opcao);

if((opcao == 'S') || (opcao == 's')){
    printf("Conta ativada.\n\n");
    ativo = 1;
}

printf("Deseja logar sua conta? S/N\n\n");
scanf(" %c", &opcao);

if((opcao == 's') || (opcao =='S')){
    printf("Conta logada.\n\n");
    logado = 1;
}

if((cadastro == 1) && ((ativo == 1) || (logado == 1))){
    printf("Tudo certo!\n\n");
} else {
    printf("Algo deu errado.\n\n");
}

return 0;
}
  • 2

    You did not initialize the variables cadastro and ativo with 0. It may be that they have the value 1 because it has the value that was already in the memory of your computer.

  • 1

    Only ZERO is false. Any other value is true. Not just 1.

1 answer

0


If your idea is to initialize the variables register, active and logged in 0 then change cadastro, ativo, logado=0;for cadastro = ativo = logado = 0;

a look at this topic on initialization of variables

Regarding the conditional that is not returning the expected result...

if((cadastro == 1) && ((ativo == 1) || (logado == 1))){
    printf("Tudo certo!\n\n");
} else {
    printf("Algo deu errado.\n\n");
}

Following the order of precedence first the inner parentheses are evaluated (cadastro == 1) && ((ativo == 1) which are False because the height of the variables register and active will be 1 and 0 and by truth table 1 and 0 = False and the variable logged will also be in 0 ie...

[(True && False) || False] = False

Below the code with a printf between the conditions for you to follow the variables at runtime.

#include <stdio.h>

using namespace std;
int main(){

int cadastro, ativo, logado;
char opcao;

cadastro = ativo = logado = 0;


printf("\n===%i %i %i\n===", cadastro, ativo, logado);

printf("Deseja cadastrar sua conta? S/N\n\n");
scanf(" %c", &opcao);

if((opcao == 'S') || (opcao == 's')){
    printf("Conta cadastrada.\n\n");
    cadastro = 1;
}

printf("\n===%i %i %i\n===", cadastro, ativo, logado);

printf("Deseja ativar sua conta? S/N\n\n");
scanf(" %c", &opcao);

if((opcao == 'S') || (opcao == 's')){
    printf("Conta ativada.\n\n");
    ativo = 1;
}

printf("\n===%i %i %i\n===", cadastro, ativo, logado);

printf("Deseja logar sua conta? S/N\n\n");
scanf(" %c", &opcao);

if((opcao == 's') || (opcao =='S')){
    printf("Conta logada.\n\n");
    logado = 1;
}

printf("\n===%i %i %i\n===", cadastro, ativo, logado);

if((cadastro == 1) && ((ativo == 1) || (logado == 1))){
    printf("Tudo certo!\n\n");
} else {
    printf("Algo deu errado.\n\n");
}

printf("\n===%i %i %i\n===", cadastro, ativo, logado);

return 0;
}
  • 2

    The innermost parentheses are the (ativo == 1) || (logado == 1).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.