0

I do have a list of string status messages. Each status message has an individual integer status code assinged to it, like this:

{0, "INITIAL STATE"},
{12, "ERROR INVERTER COMMUNICATION"},
{42, "INITIAL CHARGE"},
{158, "MAINTENANCE CHARGE"},
...

In fact the list has about 200 entries, with 256 being the maximum status code. Now I want to reference the respective string for a statuscode (int) I´m reading from the device. I have been looking at using a struct like this:

typedef struct {
    int Statuscode;
    String StatusString;
} StatusCodes;

and a definition like this:

StatusCodes MyStatuscodes[] = {
    {0, "INITIAL STATE"},
    {12, "ERROR INVERTER COMMUNICATION"},
    {42, "INITIAL CHARGE"},
    {158, "MAINTENANCE CHARGE"},
};

My questions:

  • When I get a statuscode of e.g. 12, how do I get to the respective string?
  • Is there a better way than using a struct?
  • Should I be using a "char *" instead of "String"?
  • Ideally I would like to move the list of messages references to a .h file, would that have an impact?

I think there must be an easy solution for this, but being new to C++ I´m struggling with finding a good solution, besides maybe converting to JSON and parsing this.

MacSass
  • 9
  • 3
  • A declaration of `extern StatusCodes MyStatuscodes[TBD_N]` makes sense in the .h file. Moving the definition of `StatusCodes MyStatuscodes` to a .h file does not. – chux - Reinstate Monica May 04 '22 at 22:45
  • 1
    @chux-ReinstateMonica: Updated with regard to those comments ... – MacSass May 04 '22 at 22:51
  • One question at a time, please. [ask] – Rob May 04 '22 at 23:53
  • "When I get a statuscode of e.g. 12, how do I get to the respective string?", you seems of thinking using the statsuscode as an index, but it is not the index that can get you the String, in the object that you have, the index for the status code is not 12, but 1 (index for 42 is 2). So with this in mind, it make not much difference of having a struct or two different arrays, you could have `statusCodes = {0, 12, 42, ... }` and `statusString = {"sting 0", "string 1", "sting 2"...}`. – hcheung May 05 '22 at 01:42
  • "Should I be using a `char *` instead of `String`?", I would suggest to use `const char*`. – hcheung May 05 '22 at 01:43
  • For what you have now you could use a loop, like `for(auto &&code:MyStatuscodes)if(code.Statuscode==12){;break;}`. Of course there are `find_if` variants you can try too (`std`, `ranges`). – tevemadar May 05 '22 at 10:36

1 Answers1

-1

list has about 200 entries
256 being the max. code.

Is there a better way than using a struct?

Consider an array of const char *.

Consider in some error.cpp file

#incldue "error.h"
const char *StatusString[STATUSSTRING_N] = {
  "INITIAL STATE",
  "UNUSED 1",  
  ...
  "UNUSED 11",  
  "ERROR INVERTER COMMUNICATION"
  ...
};

and in some error.h file

#define STATUSSTRING_N 257
extern const char *StatusString[STATUSSTRING_N];

When I get a statuscode of e.g. 12, how do I get to the respective string?

if (statuscode >= 0 && statuscode < STATUSSTRING_N) {
  return StatusString[statuscode];
}

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • OP's statusCode is not an index. the index of status code 12 is 1. – hcheung May 05 '22 at 01:39
  • @hcheung OP has "When I get a statuscode of e.g. 12, how do I get to the respective string?". By creating an array of `StatusString`, OP can use the statuscode as the index. instead of the artificial 1. – chux - Reinstate Monica May 05 '22 at 01:56
  • Besides the fact that I would to have to make sure I fill the "gaps" in the list of status codes with "unknown" string, I do actually like the proposal of @chux-ReinstateMonica better than having to use loops to go through all status codes to find the respective index (I´d prefer to use JSON parsing then). If there is no more elegant solution than what chux-ReinstateMonica proposed, I will accept that as an answer. Should work for me as long as I don´t have to cope with a new status code like 3467 ;-) – MacSass May 05 '22 at 13:24
  • I implemented it that way and it is working fine. Can use the status code as index, as I "filled all the gaps" in missing statuscodes with "unknown". Had to put the `#define STATUSSTRING_N 257` from the error.h. file into my main.cpp as well, because otherwise I was getting a compiler warning ... fine for me. – MacSass May 06 '22 at 10:53