【问题标题】:Restrict spaces from input in console限制控制台输入的空格
【发布时间】:2014-04-14 10:18:49
【问题描述】:

我有 3 cin 的整数。

int input1;
cin >> input;

int input2;
cin >> input2;

int input3
cin >> input3;

问题是如果我在控制台输入2 3 4,它会一次性输入所有3 个。我怎样才能防止这种情况?如果他们这样做,可能会给他们一个警告。基本上是错误输入验证。

【问题讨论】:

    标签: c++ if-statement input while-loop cin


    【解决方案1】:

    一种可能的解决方案:

    int strict_stoi(const string& s)
    {
        size_t end_pos;
        int num = stoi(s, &end_pos);
        for (size_t i=end_pos; i<s.length(); ++i)
        {
            if (!isspace(s[i]))
                throw invalid_argument("You have entered some garbage after the number!");
        }
        return num;
    }
    
    int read_number()
    {
        string s;
        getline(cin, s);
        return strict_stoi(s);
    }
    
    int read_number_with_retry(const char* prompt)
    {
        for (;;)
        {
            try
            {
                cout << prompt;
                return read_number();
            }
            catch (invalid_argument& ex)
            {
                cout << ex.what() << endl;
            }
        }
    }
    
    int test()
    {
        int input1 = read_number_with_retry("Enter input #1: ");
        int input2 = read_number_with_retry("Enter input #2: ");
        int input3 = read_number_with_retry("Enter input #3: ");
    
        return 0;
    }
    

    如果您输入了一个完全无效的参数(例如“a”),那么它会向您显示一个不太友好的“无效 stoi 参数”消息,但如果您输入“5 6”,那么它会显示“您输入了一些垃圾”在号码之后!”。如果你想用用户友好的东西替换“无效的 stoi 参数”消息,那么当你发现“数字后面的垃圾”时,而不是抛出 invalid_argument 异常,你应该抛出你自己的 garbage_after_the_number 异常,在这种情况下你可以两种不同错误之间的区别:invalid_argument 只会在输入无效(如“a”)的情况下被抛出,garbage_after_the_number 只会在另一种错误的情况下被抛出,这样你就可以捕获两个不同的错误例外,您可以在这两种情况下打印完全自定义的消息。我把它的实现留给你作为一个额外的练习。

    【讨论】:

      【解决方案2】:

      你可以这样做:

      #include <iostream>
      #include <sstream>
      
      int main() {
          while(true) {
              std::cout << "Enter a number [Enter to quit]: ";
              std::string line;
              getline(std::cin, line);
              if(line.empty()) break;
              else {
                  std::stringstream input(line);
                  int number;
                  // Preceding white space number trailing white space:
                  input >> number >> std::ws;
                  if(input && input.eof()) {
                      std::cout
                          << "The number surronded by possible white space is: "
                          << number
                          << '\n';
                  }
                  else {
                      std::cout
                          << "The input line is invalid: "
                          << line
                          << '\n';
                  }
              }
          }
      }
      

      如果你想严格:

      #include <iostream>
      #include <iomanip>
      #include <sstream>
      
      ...
      
      // Number without preceding and trailing white space:
      input >> std::noskipws >> number;
      
      ...
      

      【讨论】:

        猜你喜欢
        • 2011-10-07
        • 1970-01-01
        • 2022-06-14
        • 2012-02-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-02
        • 1970-01-01
        相关资源
        最近更新 更多