How to compare two variables in an if statement?

I'm working on a project where I need to control a door lock using a barcode scanner.

The code used to get data from the scanner:

#include <PS2Keyboard.h>

const int DataPin = 2;
const int IRQpin =  3;

int id = 1234;

PS2Keyboard keyboard;

void setup() {
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(9600);
}

void loop() {
  char c;
  if (keyboard.available()) {
    c = keyboard.read();
      Serial.print(c);
    }

    if(???); //check if the scanned code 'c' is the same as 'id'
    {
      //unlock door
    }
  }

The door should be unlocked if the the scanned barcode is the the same as the number 'id', but I don't know how to compare the 2 variables. I tried using "c == id" in the if statement but it didn't work.

    if(???); //check if the scanned code 'c' is the same as 'id'

Since c is a char, and can hold values in the range -128 to 127, it is not likely that c will ever contain 1234.

int id = 1234;

An integer, ie a number

 char c;

A single character
They are never going to be equal

Either make id an array of chars terminated with a zero to make it into a string then build up a corresponding string as the user enters the code and compare the two using strcmp() or build the user input string and convert it to a number using atoi() and compare it directly with the id

As Bob pointed out, the Serial object sends data to you one character at a time. To capture that data, try something like:

  // in loop()
   char msg[6];
   int charsReceived;
   int enteredID;

   if (Serial.available() > 0) {      // Is data being sent?
       charsReceived = Serial.readBytesUntil('\n', msg, sizeof(msg) - 1); // store in msg until a newline sent,
                                                                          // but save room for NULL 
      msg[charsReceived] = NULL;      // Make it a string
      enteredID = atoi(msg);          // Convert string to int
      if (enteredID == ID) {
         Serial.println("Match");
      } else {
         Serial.println("No Match");
   }

You can compare the char to an ASCII value as well

jasperachtbaan:
You can compare the char to an ASCII value as well

True, but the PW is multiple digits. It's probably easier to collect all the input, convert it to an int, and then compare it rather than convert the PW to ASCII and compare it one character at a time as the data arrive via the Serial object.

Or just read a string and compare as a string. (Not String, string):

char buffer [5] ;   // 4 chars and a null terminator
byte char_count = 0 ; // index for inserting in buffer

void loop() {
  char c;
  if (keyboard.available()) {
    c = keyboard.read();
    Serial.print(c);
    buffer [char_count++] = c ;  // store in buffer
    if (char_count >= 4)
    {
      buffer[char_count] = 0 ;   // null terminated string
      if (strcmp (buffer, id) == 0)  // compare strings equal
      {
	// unlock door

	char_count = 0 ;  // reset buffer for next time
      }
    }
  }
}

I thought the PW is an int.

Thanks for the help everyone!

I ended up using this horrible method, it's ugly but it works ¯_(ツ)_/¯

char id1 = '1';
char id2 = '2';
char id3 = '3';
char id4 = '4';

char c1 ;
char c2 ;
char c3 ;
char c4 ;

...

void loop() 
  char c;
   int i =0;
  while(keyboard.available()) {
    c = keyboard.read();
    delay(90);
    switch (i) {
    case 0:{c1=c;}break;
    case 1:{c2=c;}break;
    case 2:{c3=c;}break;
    case 3:{c4=c;}break;
    }
     i++;
  }
  Serial.print(c1);
  Serial.print(c2);
  Serial.print(c3);
  Serial.print(c4);

if(c1==id1 && id2==c2 && id3==c3 && id4==c4 )
{   
  Serial.print("yes");
}
else{
Serial.print("no");
}

I ended up using this horrible method, it's ugly but it works ¯_(ツ)_/¯

It is the ugliest thing I have ever seen. Try changing that crap to support 8 character passwords.

Then, think about using arrays, and how much simpler it would be to change the size of the array from 5 to 9.

Then, think about the fact that a string is just a NULL terminated array of chars, and the fact that everyone, in ALL of your posts, has been telling you to use strings.