evaluate multiple variables in a if [closed] statement

1

I've been developing a program to find passwords in a game called keep talking and nobody explodes, in which one of the challenges is to find the correct 5-letter password from a list of passwords. To solve it you are given 6 possible letters for each cell of the word (for example in the first column the letters could appear (randomly):
a
or
q
w
e

so the possible passwords are: about, again, which, where and every, because those are the passwords that start with those letters, in the following columns you are given more letters until you can determine the correct password.

I am trying to create a program in C ++ to find the password automatically, but I ran into a problem, when discarding bad passwords I end up with a code like this:

Here, for example, if none of the letters is an "a", I determine all the passwords that have the a in that position to be incorrect:

if(letra1 != a && letra2 != a && letra3 != a && letra4 != a && letra5 != a && letra6 != a){           
again, after, about = 0
} 

But this leaves me with an ugly code in which I have to use the letter I'm checking many times, try to do this:

if(letra1, letra2, letra3, letra4, letra5, letra6 != a){                        
    again, after, about = 0;
}

But C ++ seems not to read that as I expected.

Does anyone know of any way to solve this problem? Or maybe someone who can leave me a program that uses lists / arrays? I would have tried to use a list of elements, but that is very advanced for me.

If someone has a suggestion of a simpler way to achieve this, I would also appreciate it. :)

    
asked by Mat 27.07.2018 в 22:57
source

3 answers

1
  

But this leaves me with an ugly code in which I have to use the letter I'm checking many times, try to do this:

if(letra1, letra2, letra3, letra4, letra5, letra6 != a){                        
    again, after, about = 0;
}
     

But C ++ seems not to read that as I expected.

The way in which this instruction is interpreted in C ++ is the following: each of the expressions separated by a comma is evaluated and all except the last one are discarded. That is, it evaluates letra1 and discards it, letra2 and discards it, until it reaches letra6 != a and uses it as a result of the complete expression.

This is the behavior of the operator comma , .

What you intend to do is not possible in C ++, that is: there is no way to simplify that kind of comparison 1 . What you can do is compare in another way, for example by saving the letters you want to compare in a 2 formation and operating on it:

char letras[] {letra1, letra2, letra3, letra4, letra5, letra6};

if (std::count(std::begin(letras), std::end(letras), 'a') == 0) {
    // Ninguna letra es 'a'
}
  • And as far as I know, there is no language that allows it in the way you indicate.
  • Also known as an arrangement or in English: array.
  • answered by 30.07.2018 / 08:39
    source
    3

    It occurs to me that you could use a switch case:

    switch(a){
        case letra1:
        case letra2:
        case letra3:
        case letra4:
        case letra5:
        case letra6:
            break;
        default:
            again, after, about = 0;
            break;
    }
    
        
    answered by 28.07.2018 в 00:13
    0

    What you can do is create a separate method to check the condition and call it whenever you need it:

    public bool condicion(String letra){
       if(letra1 != letra && letra2 != letra && letra3 != letra && letra4 != letra && letra5 != letra && letra6 != letra){
          return true;
       }
       return false;
    }
    

    Now to use it in a method:

    if(condicion("a")){           
       again, after, about = 0
    } 
    

    And so all right.

        
    answered by 30.07.2018 в 13:48