【问题标题】:c++ - Input Validation Putting Numbers Into Vectorc++ - 将数字放入向量中的输入验证
【发布时间】:2019-03-11 23:26:40
【问题描述】:

我一直在尝试创建一个简单的 while 循环来将数字转换为向量,并进行一些基本的输入验证。但是,我发现我的代码发生了一些奇怪的事情。我试图让用户可以输入数字,直到他们输入“0”,这将结束循环。这工作正常。

但是,我也在尝试这样做,以便如果他们输入非整数(即 3.4,a),或者如果他们输入负数,它会输出“BAD INPUT”然后退出程序。当我输入负数时,它工作正常。但是当我输入一个非整数时,它什么也没有,但仍然退出程序......

同样,我也想这样做,如果他们先输入“0”,而没有事先输入任何数字,它会输出“NO NUMBERS”并退出程序。这个也退出了程序,但同样,没有任何东西。

我的代码如下:

   #include <iostream>
   #include <vector>

   //I know it is considered bad practice for the below part.
   using namespace std;

   int main() 
   {
        vector<int> numberStorage;

        while (cin) {
            int numInput = 0;

            cin >> numInput;

            if (numInput == 0) break;

            if (!cin || numInput < 0) {
                cout << "BAD INPUT" << '\n';
                return false;
            }

            if (numInput == 0 && numberStorage.size() == 1) {
                cout << "NO NUMBERS" << '\n';
                return false;
            }   
            numberStorage.push_back(numInput);
        }

        return 0;
    }

谁能帮助我并澄清我的逻辑/代码在这些输入验证中哪里出错了?感谢您的帮助。

【问题讨论】:

  • 按原样,您的程序将无法运行,因为它缺少主程序入口点。

标签: c++ validation vector


【解决方案1】:

试试这个:

#include <iostream>
#include <vector>
//I know it is considered bad practice for the below part.
using namespace std;

vector<int> numberStorage;

int main() {
  while (true) {
    int numInput = 0;

    cin >> numInput;

    if (cin.fail() || numInput < 0) {
      cout << "BAD INPUT" << '\n';
      break;
    }

    if (numInput == 0) {
      cout << "Received 0" << endl;
      break;
    }

    if (numInput == 0 && numberStorage.size() == 1) {
      cout << "NO NUMBERS" << '\n';
      return false;
    }   
    numberStorage.push_back(numInput);
  }

  return 0;
}

在原始代码中,您没有检查 cin.fail()。 当您尝试在 numInput 中存储不是 int 的值时, cin 静默失败,当您将 numInput 初始化为 0 时,它正在退出循环,就好像客户端在控制台上输入了数字零一样。

您可以通过在原始代码的零值条件上添加一个 cout 来自行检查。

干杯

【讨论】:

  • 谢谢你,rcmgleite!做到了。我很欣赏简洁的答案,以及我出错的地方的解释。
【解决方案2】:

When cin fails, it will assign value 0numInput 和你的逻辑

if (numInput == 0) break;

静默退出程序。

【讨论】:

  • 这实际上是正确的答案。我还认为while(cin) 会导致问题,但事实并非如此。
  • 这很有意义。我很惊讶我看不到这一点(我是初学者!)谢谢。
【解决方案3】:

如果您调用cin.clear() 以防出现cin.fail() 以清除cin 上的错误标志,这将是一个好习惯,以便将来的I/O 操作能够正常工作。

只是一个建议!

【讨论】:

    【解决方案4】:

    您可以在设置整数检查而不是数字后使用 cin.fail() 进行检查。

    #include <iostream>
    #include <vector>
    using namespace std;
    
    vector<int> numberStorage;
    
    int main() {
        while (cin) {
            int numInput = 0;
    
            cout << "Please enter:";
            cin >> numInput;
    
            if(cin.fail()) {
                cout << "NO NUMBER" << "\n";
            }
    
            if (numInput == 0) break;
    
            if (!cin || numInput < 0) {
                cout << "BAD INPUT" << '\n';
                return false;
            }
    
            numberStorage.push_back(numInput);
        }
    }
    

    结果如下:https://onlinegdb.com/SyIWdqU9m

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多