Briget
Briget

Reputation: 31

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.

Updated code:

char password[4] = "";
int c = 0;
int flg;

void main(void)
{
 LATC = 0x00;
 TRISC = 0x0b10000000;

 //Clear Port B for now
 LATB = 0x00;
 TRISB = 0x00;


  OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
  USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 64);


putrsUSART( " Welcome to your lock program. \r\n " ); // Send the string
Delay10KTCYx(400); // Delay between data transmissions
noPass = 1;

 while(noPass == 1)
 {
   putrsUSART(" \r\n\n Please type the password in: ");

  while (c<=2)
  {
    while(BusyUSART());
       // wait for user input
    while (!DataRdyUSART());
       //get string from terminal
      password[c] = getcUSART();       //read a byte from USART

       putrsUSART("\r\n\n You typed: "); 
       //Write a string from program memory to the USART
       putcUSART(password[c]);

       putrsUSART("\r\n\n");        
       c++;
  }

 //if (password[0] == '1' && password[1] == '2' && password[2] == '3' )
  if (strcmp(password, "123") == 0)
 //unlock
 {
 putrsUSART("\r\n\n Correct Pasword \r\n\n");
 //short time to unlock, set solenoid, then delay it, 
 //then reset it so it locks again. 
    Delay10KTCYx(1000); // Delay between data transmissions
   c = 0;
   noPass = 0;
}
  else
  {
 putrsUSART("\r\n\n Wrong password, please try again \r\n\n");
 c = 0;
  }
}

}

Upvotes: 2

Views: 185

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50775

Be aware that password[3] can contain only strings up to a length of 2 because you need to account for the NUL string terminator. But even 3 is somewhat short for a password.

So you need at least

char password[4] = "";

And you must make sure that you vener copy passwords longer that 3 characters to password.

Furthermore string comparisions are done with strcmp.

Replace

if (password == "123")

with

if (strcmp(password, "123") == 0)

But be aware that strcmp can only operate on NUL terminated strings.

So you need this (I removed your comments, all comments are mine):

  while (c<=2)
  {
    while(BusyUSART());

    while (!DataRdyUSART());
      password[c] = getcUSART();

      putrsUSART("\r\n\n You typed: "); 
      putcUSART(password[c]);

      putrsUSART("\r\n\n");        
      c++;
  }

  password[c] = 0;    // NUL terminated the password

  if (strcmp(password, "123") == 0)
  {
    // password correct
  }

Upvotes: 5

Related Questions