【问题标题】:How do I make a C++ program that filter out non-integers?如何制作过滤掉非整数的 C++ 程序?
【发布时间】:2017-03-22 00:46:15
【问题描述】:

类似的东西

cout << "Enter the number of columns: " ;
    cin >> input ; 
    while( input != int ){
      cout << endl <<"Column size must be an integer"<< endl << endl;
      cout << "Enter the number of columns: " ;
      cin >> input ;
   }

【问题讨论】:

    标签: c++


    【解决方案1】:

    cin 会为你做这件事,有点。如果cin 收到与input 不同类型的内容,它将失败。你可以这样做:

    int input;
    while(!(cin >> input))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << endl <<"Column size must be an integer"<< endl << endl;
        cout << "Enter the number of columns: " ;
    }
    

    cin.clear() 清除错误位,cin.ignore() 清除输入流。我使用number_limits 来获取流的最大大小,这需要您#include&lt;limits&gt;。或者,您可以只使用大数字或循环。

    【讨论】:

    • 谢谢,有没有什么办法可以让浮点数产生相同的响应?
    • 这个答案是正确的,但应该补充一点:在此之前您需要将变量“input”声明为整数类型,例如int input;
    • 要剪切所有这些样板代码,你可以使用这个函数:stackoverflow.com/questions/2256527/…
    • @Anteater - 此代码适用于任何类型。 cin 期望接收与传递给它的变量类型相同的输入(在这种情况下为input)。我已经编辑了我的代码,以便将 input 声明为 int,但您可以轻松地将其声明为 float
    【解决方案2】:

    你不能那样做; input 必须有一些具体的类型。

    可行的最简单方法是从cin 读取字符串,然后在第二步中使用strtol 或其亲属之一将其转换为整数,并发出错误如果strtol 没有消耗整个字符串,则消息。

    【讨论】:

      【解决方案3】:
       #include<iostream.h>
       using namespace std;
       int main()
       { 
      int x;
      int input;
        while(!0){
             cout<<"Enter your option :";
             cout<<"1 .Enter Column size :"<<endl;
              cout<<"2.Exit "<<endl;
            cin>>x;
           switch(x)
               {
                  case 1:   cout << "Enter the number of columns: "<<endl ;
                            cin>>input;
                           if(input>0)
                             cout << "The number of columns: is "<<input<<endl ;
                           else 
                             cout << "Enter the number of columns as integer  "<<endl ;
      
                   case 2:exit(0);            
              }
         };
       return 0;
      }
      

      【讨论】:

        【解决方案4】:

        这里的许多答案都使用了 cin 的内置过滤器。虽然这些可以防止输入字符或字符串,但它们不会阻止浮点输入。当输入一个浮点数时,它被接受并且十进制值保留在缓冲区中。这会在以后对 cin 的请求中产生问题。以下代码将检查 cin 错误标志并防止浮点输入。

        *注意:cin.ignore 语句可能需要进行一些调整才能完全验证代码。

        void main()
        {
            int myint;
        
            cout<<"Enter an integer: ";
            intInput(myint); 
        }
        
        void intInput(int &x)
        {
            bool valid = true;  //flag used to exit loop
        
            do
            {
                cin>>x;
        
                //This 'if' looks for either of the following conditions:
                //cin.fail() returned 'true' because a char was entered.
                //or
                //cin.get()!='\n' indicating a float was entered.
                if(cin.fail() || cin.get()!='\n')
                {
                    cout<<"Error.  The value you entered was not an integer."<<endl;
                    cout<<"Please enter an integer:  ";
                    cin.clear();            //clears cin.fail flag
                    cin.ignore(256,'\n');   //clears cin buffer
                    valid = false;          //sets flag to repeat loop
                }
                else valid = true;          //sets flag to exit loop
            }while(valid == false);     
        }
        

        【讨论】:

          【解决方案5】:

          这是解决您的问题的一个非常基本的解决方案,新手程序员应该会发现它对试图破坏他们的程序的人有用。最终会有更先进和更有效的方法来做到这一点。

          int input;
          int count = 1;
          while(count == 1){  //this is just a simple looping design
              cin >> input;
              if(cin.fail()){     //If the input is about to crash your precious program
                  cin.clear();       //Removes the error message from internal 'fail safe'
                  cin.ignore(std::numeric_limits<int>::max(), '\n');     //Removes the bad values creating the error in the first place
                  count = 1;  //If there is an error then it refreshes the input function
              }
              else{
                  count--;  //If there is no error, then your program can continue as normal
              }
          }
          

          这里是高级代码:stackoverflow.com/questions/2256527/

          【讨论】:

          • 注意:我编辑了您的答案,但我需要在 else 之前添加一个额外的括号,因为您的逻辑有点不对劲。这是您应该缩进代码的一个很好的理由。但是,如果这是错误的,请随时重新编辑。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-13
          • 1970-01-01
          • 1970-01-01
          • 2014-10-03
          • 2011-04-03
          • 1970-01-01
          • 2018-09-02
          相关资源
          最近更新 更多