【问题标题】:CIN within certain range一定范围内的CIN
【发布时间】:2014-04-23 03:42:21
【问题描述】:

我正在尝试制作一个用户只能输入 0 到 1 的 cin。如果用户没有输入这些数字,那么他应该会收到一条错误消息“请在 0 到 1 的范围内输入。”

但它不起作用。

我做错了什么?

   int alphaval = -1;
    do
    {
        std::cout << "Enter Alpha between [0, 1]:  ";
        while (!(std::cin >> alphaval)) // while the input is invalid
        {
            std::cin.clear(); // clear the fail bit
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore the invalid entry
            std::cout << "Invalid Entry!  Please Enter a valid value:  ";
        }
    }
    while (0 > alphaval || 1 < alphaval);

    Alpha = alphaval;

【问题讨论】:

  • 什么'不起作用'?
  • “0 到 1”,或“0 或 1”
  • 0 或 1 如果我不输入,它不会问我错误
  • 您需要更具体地了解问题所在。该代码似乎对我来说很好用。由于字符串“请在 0 到 1 的范围内输入”。没有出现在您发布的代码中,没有打印出来也就不足为奇了。
  • @jrd1 是的,就是这样!我无法让它正常工作

标签: c++ validation stdin


【解决方案1】:

试试这个:

int alphaval;
cout << "Enter a number between 0 and 1: ";
cin >> alphaval;
while (alphaval < 0 || alphaval > 1)
{
        cout << "Invalid entry! Please enter a valid value: ";
        cin >> alphaval;
}

【讨论】:

    【解决方案2】:

    如果你想捕获空行,我会使用std::getline,然后解析字符串以查看输入是否有效。

    类似这样的:

    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main()
    {
        int alphaval = -1;
        for(;;)
        {
            std::cout << "Enter Alpha between [0, 1]:  ";
    
            std::string line;
            std::getline(std::cin, line);
            if(!line.empty())
            {
                std::stringstream s(line);
                //If an int was parsed, the stream is now empty, and it fits the range break out of the loop.
                if(s >> alphaval && s.eof() && (alphaval >= 0 && alphaval <= 1))
                {
                    break;
                }
            }
            std::cout << "Invalid Entry!\n";
        }
        std::cout << "Alpha = " << alphaval << "\n";
    
        return 0;
    }
    

    如果您想在错误时使用不同的提示,那么我会将初始提示放在循环之外,并将内部提示更改为您喜欢的提示。

    【讨论】:

      【解决方案3】:

      C++ 第一周,从Peggy Fisher's Learning C++ on Lynda.com 开始。 这就是我想出的。喜欢收到反馈。

      int GetIntFromRange(int lower, int upper){
          //variable that we'll assign input to
          int input; 
          //clear any previous inputs so that we don't take anything from previous lines
          cin.clear(); 
          cin.ignore(numeric_limits<streamsize>::max(), '\n');
      
          //First error catch. If it's not an integer, don't even let it get to bounds control
          while(!(cin>>input)) {
              cout << "Wrong Input Type. Please try again.\n";
              cin.clear();
              cin.ignore(numeric_limits<streamsize>::max(), '\n');
         }
      
          //Bounds control
          while(input < lower || input > upper) {
              cout << "Out of Range. Re-enter option: ";
              cin.ignore(numeric_limits<streamsize>::max(), '\n');
      
              //Second error catch. If out of range integer was entered, and then a non-integer this second one shall catch it
              while(!(cin>>input)) {
                  cout << "Wrong Input Type. Please try again.\n";
                  cin.clear();
                  cin.ignore(numeric_limits<streamsize>::max(), '\n');
              }
          }
      
          //return the cin input
          return input;
      }
      

      因为练习是订购汉堡包,所以我是这样要求数量的:

      int main(){
          amount=GetIntFromRange(0,20);
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-13
        • 1970-01-01
        • 2017-01-16
        • 1970-01-01
        • 2021-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多