-1

I have a table in the form of driver start number and driver name. Now I want to determine the driver name based on the start number. Which solution approach can I follow for this?

20 - driver name A

44 - driver name B

4 - driver name C

......

That would be my approach:

String ReturnDriver(int startNumber) {
  Switch(startNumber) {
  case 20:
    {
      return "driver name A";
    }
    break;
  case 44:
    {
      return "driver name B";
    }
    break;
  case 4:
    {
      return "driver name C";
    }
    break;
  }
}
sadrick
  • 13
  • 4
  • Show some code please. It's not clear what you're asking. Give concrete example inputs and outputs. Your question as asked can only be answered as, "From the table, get the name that corresponds to the number." – JohnFilleau Jan 24 '23 at 21:39

1 Answers1

0

Would be nice if Arduino had something similar to key/value pair dictionaries. I'm not sure what your data looks like, but I wrote a little function that can emulate key/value pairs using a string of names and numbers:

String getName(int n){
  int start = input.indexOf(":",input.indexOf(String(n)));
  int end = input.indexOf(",", start++);
  if(end < 0) end = input.length();
  String value = input.substring(start, end);
  value.trim();
  return value;
}

You can change the separating character to be whatever you need.
You can write some code to combine the data you have into a string like so:

String input = "20: Driver,"
              "4: Name,"
              "23: David";

or

String input = "20: Driver, 4: Name, 23: David";

find a name in a string:
Serial.print(getName(4));
> Name

Or if your drivers occupy all positions, just order them in an array:

//each element has an id starting from 0
String name[] = {"Name", "Driver", "David"};

position = 1; //array element 2

Serial.print(name[position]);
Roman
  • 124
  • 6