Having problems with "else if" statement

Because the size of an int is 2.

Please be assured that it does.

Yes! It is the pgm_read_byte/word() function. Trying to figure out how to read back the CardName structure data from flash and then save them into RAM before comparison.

What a waste of time!
That's why we have strcmp_P

Cheers!
Using your declaration (for CardName structure's data in flash) and the strcpy_P() function (not strcmp_P() @TheMemberFormerlyKnownAsAWOL ), I have managed to make the codes of Post-30 working.

The full codes are:

struct CardName
{
  const char cardId[20];
  const char userName[15];
};

const CardName registeredCards[] PROGMEM
{
  {"214:136:105:109", "Steve"},
  {"006:159:111:109", "Oma"},
  {"059:173:223:018", "KeyRing"}
};

const int nCards = sizeof registeredCards / sizeof(registeredCards[0]);// [registeredCards];

char arr1[20] = "";
char arr2[20] = "";
char arr3[20] = "";
char *arrays[3] = {arr1, arr2, arr3};
int i;

void compare(const char* buffer)
{
  int found = -1;
  Serial.print (F("Card "));
  Serial.print (buffer);
  for (i = 0; i < nCards; i++)
  {
    strcpy_P(arrays[i], registeredCards[i].cardId);
    if (strcmp(buffer, arrays[i]) == 0)
    {
      found = i;
      break;
    }
  }

  if (found >= 0)
  {
    Serial.print (F(" belongs to "));
    strcpy_P(arrays[i], registeredCards[found].userName);
    Serial.println (arrays[i]);
  }
  else
  {
    Serial.println (F(" NOT FOUND"));
  }
}

void setup ()
{
  Serial.begin (115200);
  compare ("059:173:223:018");
  compare ("059:173:223:017");
  compare ("214:136:105:109");
  compare ("006:159:111:109");
}

void loop ()
{}

The program ouput:

Card 059:173:223:018 belongs to KeyRing
Card 059:173:223:017 NOT FOUND
Card 214:136:105:109 belongs to Steve
Card 006:159:111:109 belongs to Oma

I would appreciate to see a version of the program using strcmp_P() function.

I don't get this bit.
If the data is in flash memory, why do you want/need it in RAM too?

As I could not manage to implement your suggested function - the strcmp_P() for direct manipulation, I have read the data item from flash into RAM and then have made the comparison using the strcmp() function.

Would appreciate receiving clue/hint how to use strcmp_P() function. The Google has not helped me much.

Even if you can't use strcmp_P, it's crazy to copy all the data from flash; just copy them one at a time into some scratchpad RAM.

Edit: I don't have a computer here, but what's wrong with

  for (int i = 0; i < nCards; i++)
  {
    if (strcmp_P(buffer, registeredCards[i].cardId) == 0)
    {
      found = i;
      break;
    }
  }

Chers! That works!

struct CardName
{
  const char cardId[20];
  const char userName[15];
};

const CardName registeredCards[] PROGMEM
{
  {"214:136:105:109", "Steve"},
  {"006:159:111:109", "Oma"},
  {"059:173:223:018", "KeyRing"}
};

const int nCards = sizeof registeredCards / sizeof(registeredCards[0]);// [registeredCards];
char msg[20] = "";

void compare(const char* buffer)
{
  int found = -1;
  Serial.print (F("Card "));
  Serial.print (buffer);
  for (int i = 0; i < nCards; i++)
  {
    strcpy_P(msg, registeredCards[i].cardId);
    if (strcmp(buffer, msg) == 0)
    {
      found = i;
      break;
    }
  }

  if (found >= 0)
  {
    Serial.print (F(" belongs to "));
    strcpy_P(msg, registeredCards[found].userName);
    Serial.println (msg);
  }
  else
  {
    Serial.println (F(" NOT FOUND"));
  }
}

void setup ()
{
  Serial.begin (115200);
  compare ("059:173:223:018");
  compare ("059:173:223:017");
  compare ("214:136:105:109");
  compare ("006:159:111:109");
}

void loop ()
{}
   Serial.println (( __FlashStringHelper *)registeredCards[found].userName);

Ought to be simpler.

(someone can probably come up with a better /safer cast)

It works now! Why was it not working for the last few hours? Probably, I could not think that I would need the following line to print the corresponding userName.

Serial.println(strcpy_P(buffer, registeredCards[found].userName));

Cheers again!

Serial.println ((` __FlashStringHelper *)`registeredCards[found].userName);

It also works; however, I see it as a bureaucratic jargon!

Perhaps one of the fancy C++ casts would be "better" ... I can't keep them straight.

Regardless, a cast is definitely needed. Otherwise, it may only "work" because the compiler takes pity on you.

Haha, that has literally never happened to me.

a7

...as noted in reply #60

Agree, "clever" IT can become a real pain ... :innocent: :cry: