I intend to accept all of the following answers:
- y
- Y
- yes
- YES
- Yes
- plus mixtures, such as yeS, yES, which I didn’t intend to accept but it’s a consequence of my code
- the same applies to No and its variants
#include <iostream> // std::cout, std::cin #include <string> // std::string #include <algorithm> // std::transform int main() { bool answer_valid = false; std::string answer; do { std::cout << "Should the password include digits? [Y/n] "; std::cin >> answer; std::transform(answer.begin(), answer.end(), answer.begin(), ::tolower); answer_valid = (answer == "y") || (answer == "n") || (answer == "yes") || (answer == "no"); } while (!answer_valid); return 0; }
It works flawlessly, or it seems so. Is there better approach?