32

In the following loop, if we type characters as the cin input instead of numbers which are expected, then it goes into infinite loop. Could anyone please explain to me why this occurs?

When we use cin, if the input is not a number, then are there ways to detect this to avoid abovementioned problems?

unsigned long ul_x1, ul_x2;

while (1)
{
  cin >> ul_x1 >> ul_x2;
  cout << "ux_x1 is " << ul_x1 << endl << "ul_x2 is " << ul_x2 << endl;
}

4 Answers 4

42

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<std::streamsize>::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.

Sign up to request clarification or add additional context in comments.

5 Comments

Or better yet, a stringstream.
If I call cin.clear() after cin fails, why it still doesn't ask for an input the next time I call cin >> x ?
This answer doesn't fix the problem OP met as it is incomplete. Please take a look at my answer:)
@Eric, I fixed the bug in my answer. Thanks for pointing it out.
@Almo, you are probably getting burned because max is a macro defined in windows.h somewhere. So its overriding whats in the C++ std lib. Try doing #undef max before the cin.ignore and see what happens.
16

Attention

Please pay attention to the following solution. It is not complete yet to clear the error in your case. You will still get an infinite loop!

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

Complete Solution

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<std::streamsize>::max(), '\n');
}

Another Solution

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

1 Comment

Keep in mind that the program will proceed with the input even if you call those functions. You need to throw all of that into a while() loop and call cin on the input at the end.
2

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 :)

Comments

1

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<std::streamsize>::max(), '\n');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.