0

I'm a total beginner to C++, and I am trying to code a program that checks user input to make sure it is a valid option.

Here's my code so far:

#include <iostream>
#include <string>
#include <tuple>

int UserInputCheck() {
    int x;
    cout << "Options: 1,2,3 or q. \n \n Choose an option:";
    cin >> x;
    tuple<int,int,int,string> valid_options{ 1, 2, 3, "q"};
    do {
        cout << "\nInvalid input. Please try again.";
        cin >> x;
    }
    while (x is not in valid_options); // This is psuedo-code, I'm looking for a function that does this
    
    cout << x << "\n";
    return 0;
{

So is there a C++ function that would check if x is in valid_options?

If not, how can I write one?

433MEA
  • 100
  • 8
  • 4
    You can write one, but your question is moot. `int x; std::cin >> x;` can never put `"q"` in `x`. – Yksisarvinen Jun 15 '22 at 22:46
  • 2
    Seems to me like you could achieve much the same result with `char valid_options[] = { '1', '2', '2', 'q' };` (obv then x would be a `char` as well) at which point a simple loop would do. – Borgleader Jun 15 '22 at 22:51
  • 1
    Honestly, introducing a `tuple` to simply check if single character input is valid is overkill, and frankly, obfuscating. Seems like you're trying to hammer a tack into a wall with a piledriver. – PaulMcKenzie Jun 15 '22 at 22:57
  • There can be such a thing as [trying to be] too clever in C++. Don't fall for it, try to write code that is easy to follow and, ideally, demonstrably correct. – Paul Sanders Jun 15 '22 at 22:59
  • @PaulMcKenzie Can you provide an example of how to check the input otherwise? – 433MEA Jun 15 '22 at 23:02
  • 1
    If you know Python and are trying to learn C++ now, forget everything you learned about Python. Drawing parallels between these languages is going to bring you only woe and misery. Try to get [a good beginner C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn from. – Yksisarvinen Jun 15 '22 at 23:02
  • 1
    @433MEA -- Is what #Yksisarvinen mentions correct, that you are using Python (or similar language) as a model in writing C++ code? If so, don't do this. It is also strange why you plucked `std::tuple` out of seemingly thin air, since you say you're a beginner in C++. If you continue to go down the road of using another language as a model in writing C++, your code will suffer from: 1) Bugs, 2) Inefficiency, and 3) The program will look totally weird to a C++ programmer. Your attempt falls into category 3). – PaulMcKenzie Jun 15 '22 at 23:06

1 Answers1

1
static inline bool isValid(int input) {
        static const std::vector<int> valids = {1, 2, 3};

        return std::any_of(valids.begin(), valids.end(),
                                 [&input](const auto &s) { return input == s; });
    }

This function can do the coffee

EDIT: String version

static inline bool isValid(const std::string &input) {
static const std::vector<std::string> valids = {"1", "2", "3", "q"};

        return std::any_of(valids.begin(), valids.end(),
                                 [&input](const auto &s) { return input.find(s) != std::string::npos; });
}
X6Entrepreneur
  • 971
  • 2
  • 10
  • 30