As Thaddeus suggested, you can write a huge if/else if line, or in some cases a for loop with callbacks would be a better choice.
typedef enum {
SCN_ABCD,
SCN_HELLO,
SCN_WORLD,
} StringChoiceNumbers;
typedef struct {
StringChoiceNumbers eCase;
LPCSTR szCase;
} StringChoice;
StringChoice scOptions[] = {
{ SCN_ABCD, "abcd" },
{ SCN_HELLO, "hello" },
{ SCN_WORLD, "world" },
};
void ProcessStringOption(StringChoiceNumbers eOption) {
switch (eOption) {
case SCN_ABCD:
break;
case SCN_HELLO:
break;
case SCN_WORLD:
break;
}
}
for (int i = 0; i < ARRAYSIZE(scOptions); ++i) {
if (strcmp(scOptions[i].szCase, p) == 0) {
ProcessStringOption(scOptions[i].eCase);
}
}
From a processing point of view it is slower, however it improves code locality.
You may no be able to use this if you need a large number of variables from the calling function.
This can also be done without any of the
enum
bit and use an array of LPSTR instead of creating a structs, and just assign a plain integer to each string to simplify it a bit.
LPCSTR szOptions[] = {
"abcd",
"hello",
"world",
};
void ProcessStringOption(DWORD nOption) {
switch (nOption) {
case 0:
break;
case 1:
break;
case 2:
break;
}
}
for (int i = 0; i < ARRAYSIZE(szOptions); ++i) {
if (strcmp(scOptions[i], p) == 0) {
ProcessStringOption(i);
}
}