Consider the following situation:
#define YES "y"||"Y"||"yes"||"Yes"||"YES"
#define NO "n"||"N"||"no"||"No"||"NO"
With macros being used at the user input.
FRW::writeLine(PLAY_AGAIN);
latestResponse = FRW::getUserInput();
if (latestResponse == YES)
{
retry = false;
}
else if (latestResponse == NO)
{
retry = true;
}
I am aware that this is wrong and I should actually use...
#define YES latestResponse == "y"|| latestResponse == "Y"|| latestResponse == "yes"|| latestResponse == "Yes"|| latestResponse == "YES"
... and checking as
if (latestResponse == YES)
{
retry = false;
}
else if (latestResponse == NO)
{
retry = true;
}
Please, can somebody recommend any other way of implementing string macros? Or should I stay this way?
There's no benefit in using macros this way.
You should rather ask, what is the proper abstraction here? In this case it could be
bool is_yes(const std::string &response) {
return response == "y" || response == "yes" || ...;
}
bool is_no(const std::string &response) {
// ...
}
and then using this in your code.