代码之家  ›  专栏  ›  技术社区  ›  ashamedgap

获取多个输入并验证它们c++

  •  0
  • ashamedgap  · 技术社区  · 2 年前

    我有多个枚举,它们的作用就像程序的设置一样。我想一个接一个地从用户输入中提取它们,并不断要求用户在某个设置上输入,直到给出有效的输入,但我觉得我的方法重复了很多代码。有更好/更聪明的方法吗?

    代码:

    #include <iostream>
    #include <limits>
    
    enum Input1 {Type1 = 1, Type2 = 2, Type3 = 3, Type4 = 4};
    enum Input2 {Type11 = 5, Type22 = 17};
    
    bool validate_input(Input1 input1, Input2 input2 = Type11) {
      if(input1 != Type1 && input1 != Type2 && input1 != Type3 && input1 != Type4) {
        return false;
      }
      
      if(input2 != Type11 && input2 != Type22) {
        return false;
      }
      
      return true;
    }
    
    int main()
    {
      int iInput1, iInput2, iInput3;
      Input1 eInput1;
      Input2 eInput2;
      bool return_val;
      
      do {
        std::cout << "Select option: 1. 2. 3. 4.";
        std::cin >> iInput1;
    
        if (std::cin.fail()) {
          std::cin.clear();
          std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    
        eInput1 = (Input1) iInput1;
    
        return_val = validate_input(eInput1);
        if (!return_val) {
          std::cout << "Invalid input!\n";
        }
      } while (!return_val);
      
      do {
        std::cout << "Select option: 1. 2.";
        std::cin >> iInput2;
    
        if (std::cin.fail()) {
          std::cin.clear();
          std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    
        eInput2 = (Input2) iInput2;
    
        return_val = validate_input(eInput1, eInput2);
        if (!return_val) {
          std::cout << "Invalid input!\n";
        }
      } while (!return_val);
    
      return 0;
    }
    
    1 回复  |  直到 2 年前
        1
  •  2
  •   X3R0    2 年前

    您可以使用通用模板并简化此代码,这些代码将得到优化,更易于阅读。

    #include <iostream>
    #include <limits>
    
    enum Input1 { Type1 = 1, Type2 = 2, Type3 = 3, Type4 = 4};
    enum Input2 { Type11 = 1, Type22 = 2};
    
    bool validate_input(Input1 input1, Input2 input2 = Type11) {
        return (input1 >= Type1 && input1 <= Type4) && (input2 == Type11 || input2 == Type22);
    }
    
    template <typename T>
    T get_valid_input(const std::string& prompt, const std::string& error_message = "Invalid input! Try again.") {
        T input;
        do {
            std::cout << prompt;
            std::cin >> input;
    
            if (std::cin.fail()) {
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            } else {
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                return input;
            }
    
            std::cout << error_message << std::endl;
        } while (true);
    }
    
    int main() {
        Input1 eInput1;
        Input2 eInput2;
    
        eInput1 = get_valid_input<Input1>("Select option: 1, 2, 3 or 4:", "Invalid input for input1! Must be between 1 and 4.");
    
        eInput2 = get_valid_input<Input2>("Select option: 1 or 2:", "Invalid input for input2! Must be 1 or 2.");
    
        std::cout << "Valid inputs: " << static_cast<int>(eInput1) << ", " << static_cast<int>(eInput2) << std::endl;
    
        return 0;
    }