# Array question.

Hello,

I'm having a bit of a struggle getting my head around a small problem im having.

I am trying to write a program that allows the user to enter in values which are stored in arrays. These numbers are a unique ID.
The entered data consists of 4 numbers between 10 and 999.
The values are stored like this.

int ID1[] = {171, 38, 239, 26};
int ID2[] = {105, 54, 171, 213};
int ID3[] = {194, 96, 17, 28};
int ID4[] = {78, 67, 68, 209};
int ID5[] = {138, 014, 113, 151};

I now need to write code that will use a for loop that will check EVERY array element against a scanned ID.

The for loop will run dependent on the number of cards entered which is manually stored in a variable called IDs.

So I want to check each element of the scanned ID which is stored in a similar array against each of the same element in every stored ID sort of like the pusdo code below.

int IDs = 5;
for (int i = 0; i < IDs; i++) {
int j = 0;
boolean match = true;
while (j < rfid.uid.size)
{
if (!(storedidByte[j] == scannedID[j]))
{
match = false;
}
i++;
}
}

Whats the best way to do this?

Do I need a multi dimensional array?

A multidimensional array is the way to go.

Instead of comparing each byte of the uid in your code, you can look at memcmp which does the hard work for you

Here's a quick piece I wrote, because I am weak on arrays...

You could probably simplify it by using a struct for the name and their digits, and I could have put the printout in a single line using printf(), but the way I did it would lead to testing each digit in the card with each digit of the persons valid card digits.

(In fact, if someone wants to improve on my reply and show how to use structs, I would appreciate it).

void setup() {

Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();

char names[][6] =           //Array size is number of names +1
{ "Jim",
"Wally",
"Eddie",
"June",
"Sally"
};

int ident[] = {
171, 38, 239, 26,        // ID1, Jim
105, 54, 171, 213,       // ID2, Wally
194, 96, 17, 28,         // ID3, Eddie
78, 67, 68, 209,         // ID4, June
138, 014, 113, 151       // ID5, Sally
};

int digit1;     // The digits will be extracted into these four ints.
int digit2;     // I am assuming that your card gives you four ints, so
int digit3;     // you just test them one at a time.
int digit4;

int idNum = 0;    // 0 to 4 in this case, for a total of five.
int numOfIds = 5;
int pointer = 0;  // =idNum * 4, is the start of a ID in the array

for (int idNum = 0; idNum < numOfIds; idNum++) {
digit1 = ident[pointer++];
digit2 = ident[pointer++];
digit3 = ident[pointer++];
digit4 = ident[pointer++];  // We now have all four digits

//Test the ID digits against the scanned card.
Serial.print(F("ID= "));
Serial.print(idNum + 1);
Serial.print(F("= "));
Serial.print(digit1);
Serial.print(F(", "));
Serial.print(digit2);
Serial.print(F(", "));
Serial.print(digit3);
Serial.print(F(", "));
Serial.print(digit4);
Serial.print(F(" - "));
Serial.print(names[idNum]);
Serial.println();

}
}

void loop() {
}

SteveMann:

char names[][6] =           //Array size is number of names +1

The size of the array (the []) is the number of names, not the number of names plus 1. You can print it out with the following:

Serial.print("Number of names: ");
Serial.println(sizeof(names) / sizeof(names[0]));