-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;
}
You did not initialize the variables
cadastro
andativo
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.– Andre
Only ZERO is false. Any other value is true. Not just 1.
– arfneto