0
Follow
4
View

How to validate integer input vs character input [duplicate]

donglei2860 注册会员
2023-01-26 05:53

Perhaps, it's because,

  1. cin is a streaming function. When a non-numeric is entered instead of numbers, it is ignored. And you are prompted for re-entry.
  2. Even if you did give numeric inputs, you will be prompted for more inputs since you're on an infinite loop.

You can solve this problem like this: 1. Create a function to take in a string input. 2. return it as numeric after conversion. Use strtod() for conversion.

Hope this helps :)

dodo266q 注册会员
2023-01-26 05:53

Another alternative is operator! ,it is equivalent to member function fail()

//from Doug's answer
if ( !cin )
{
   cout << "ERROR -- You did not enter an integer";

   // get rid of failure state
   cin.clear(); 

   // From Eric's answer (thanks Eric)
   // discard 'bad' character(s) 
   cin.ignore(std::numeric_limits::max(), '\n');
}
z999ok 注册会员
2023-01-26 05:53

Well you always will have an infinite loop, but I know what you really want to know is why cin doesn't keep prompting for input on each loop iteration causing your infinite loop to freerun.

The reason is because cin fails in the situation you describe and won't read any more input into those variables. By giving cin bad input, cin gets in the fail state and stops prompting the command line for input causing the loop to free run.

For simple validation, you can try to use cin to validate your inputs by checking whether cin is in the fail state. When fail occurs clear the fail state and force the stream to throw away the bad input. This returns cin to normal operation so you can prompt for more input.

  if (cin.fail())
  {
     cout << "ERROR -- You did not enter an integer";

     // get rid of failure state
     cin.clear(); 

     // From Eric's answer (thanks Eric)
     // discard 'bad' character(s) 
     cin.ignore(std::numeric_limits::max(), '\n');

  }

For more sophisticated validation, you may wish to read into a string first and do more sophisticated checks on the string to make sure it matches what you expect.

dq89163756 注册会员
2023-01-26 05:53

Please pay attention to the following solution. It is not complete yet to clear the error in your case.

if (cin.fail())
{
     cout << "Please enter an integer";
     cin.clear();
}

The reason is you need clear the failed state of stream, as well as discard unprocessed characters. Otherwise, the bad character is still there and you still get infinite loops. You can simply can std::cin.ignore() to achieve this. For example,

if (cin.fail())
{
         cout << "Please enter an integer";
         // clear error state
         cin.clear();
         // discard 'bad' character(s)
         cin.ignore(std::numeric_limits::max(), '\n');
}

You can also use getline and stringstream to achieve. Here is a brief example.

   string input;
   while (1)
   {
      getline(cin, input);
      stringstream(input) >> x;
      cout << x << endl;
   }

About the Author

Question Info

Publish Time
2023-01-26 05:53
Update Time
2023-01-26 05:53