How to pick a part of a table according to the users's input?

Hey hey,
I've been looking through the Arduino forum and other website for an answer to my problem but i didn't find anything
So my question is:
If I have a structure like:

struct example {
char brand[8];
int dataSize;

int temp[BTT];

char valMode[3][BTT];
{...}

};

How can I have an access to a setting according to the user input (instead of doing several if loops)
For example if I get "Temp" from the user (or a char variable that I can modify), is there a way to use it to call example.Temp instead of:
if (strcmp(data, "temp") == 0)
Serial.println (example.Temp[0]);
Because I have a lot of variables in my sructure and i don't want to write a loop for each of them.

Cheers :slight_smile:

How about an array of valid responses, that you can iterate through with a "for" loop, there being no such thing as an "if loop"?

You can use a for loop, I have this on my code, using the flash memory because the size of the table, have a look.

struct COMMAND_STRUCT {
  const int CODE;
  const char NAME[32];
};

const COMMAND_STRUCT COMMAND_TABLE[] PROGMEM = {
  {101, "Hello"}
};

  char *command = "101";
  byte commandTableSize = sizeof(COMMAND_TABLE) / sizeof(COMMAND_STRUCT);
  for (byte i = 0; i < commandTableSize; i++) {
    if (atoi(command) == pgm_read_word(&COMMAND_TABLE[i].CODE)) {
      strcpy_P(command, COMMAND_TABLE[i].NAME);
      break;
    }

First, the struct definition you provided and the subsequent access to it, is not correct. However, I'll assume that just because you weren't really posting the code. Second, C is case sensitive, so example.Temp[0] isn't going to work. Third, you could devise some form of hash code that would index into the structure. However, hash tables often have huge holes in them and you don't have a lot of memory to waste. Finally, you wouldn't have to write an if statement block for each string. You'd write a function that receives the string to locate and pass back the index of where's it's found, or, perhaps, -1 if it is not found.

I can't figure what you are trying to do - you are just describing a "how" problem.

If you want a system in which a user can arbitarily send an item of data that needs to be compared with one of several different data items already in the Arduino maybe you need a different approach.

How, for example, would the user identify which data he is sending?

Is the data being sent from another program or by a user typing at a keyboard ?

Have you considered using a union so that the struct can be addressed in different ways for different purposes ?

...R

oh ok I Think I don't describe my question correctly
And no I didn't write my entire code because it would be too long and I have a lot of functions Calling each other
Thank you Arank but I don't want to replace the data in my structure by the data given by the user, i want to access a part of my structure according to the char that i give

so (this is definitely wrong, but this is to explain how i want it to work)

char example[] = "temperature";
then I would like to be able something like:
name_of_my_structure.example = 101;

And my proper structure is:
#define BTT 8

struct AC {
char brand[8];
int dataSize;

int bttTemp[BTT];
char valTemp[17][BTT];

int bttMode[BTT];
char valMode[3][BTT];

char modeAuto[211];

int bttFan[2][BTT];
char valFan[3][BTT];

int bttSwing[BTT];
char valSwing[6][BTT];

};

AC newAC;

Adeline0601:
so (this is definitely wrong, but this is to explain how i want it to work)

char example[] = "temperature";
then I would like to be able something like:
name_of_my_structure.example = 101;

You are still focused on HOW rather than WHAT.

And you have not answered two of my questions in Reply #4. I will repeat them

How, for example, would the user identify which data he is sending?

Is the data being sent from another program or by a user typing at a keyboard ?

...R