开发者

How do I make a C++ program that filter out non-integers?

Something like this

cout << "Enter 开发者_StackOverflow中文版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 ;
   }


cin will do this for you, kind of. cin will fail if it receives something that is not of the same type as input. What you can do is this:

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: " ;
}

The cin.clear() clears the error bits, and cin.ignore() clears the input stream. I'm using number_limits to get the maximum size of the stream, that requires that you #include<limits>. Alternatively you can just use a big number or a loop.


You can't do it like that; input has to have some concrete type.

The simplest approach that will work is to read a string from cin, then convert it to an integer in a second step with strtol or one of its relatives, and issue an error message if strtol doesn't consume the whole string.


 #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;
}


Many of the answers here use the cin's built in filter. While these work to prevent a char or string from being entered, they do not prevent a float entry. When a float is entered, it is accepted and the decimal value remains in the buffer. This creates problems with later requests to cin. The following code will check the cin error flag and also prevent float inputs.

*note: The cin.ignore statement may require some tweaking to fully bullet proof the code.

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);     
}


This is a very basic solution to your problem that newer programers should find useful for people trying to break their programs. Eventually there are more advanced and efficient ways to do this.

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
    }
}

Here is the advanced code: stackoverflow.com/questions/2256527/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜