jeudi 12 avril 2018

How to check if the following array contains these characters?

So I have an array called password. I initiate the array as: char password[3] = "" and then fill it in using a while loop which takes one character at a time. The correct password is 123.

The following statement for checking if the password is 123 works:

 if (password[0] == '1' && password[1] == '2' && password[2] == '3')

But I wanted to use something like:

if (password[3] == {1,2,3})

It is less lines and more clear to understand. I seem to get syntax error with this particular one. I have also tried

if (password == "123")

but it doesn't accept 123 as the correct password. I am not sure if it is the same in c, but the string would end with slash 0 in c++, so I tried to add that at the end as well, again didn't work.

Is there a way to write it so that I don't have to use the AND gates?

Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire