cin.clear() help?

Can anyone help me understanding why this cin-check does not work?

double get_dbl()
{
       double dbl = 0;
       while (!(cin>>dbl))
       {
             cin.clear();
             cout<<"input a number please";
             }
       return dbl;
}

int main()
{
      double dbl = get_dbl();

keep_window_open();
return 0;
}
Remove the incorrect characters in the input buffer with std::istream::ignore() before attempting the next read. http://www.cplusplus.com/reference/iostream/istream/ignore/

Something like:
1
2
3
4
5
6
       while ( !(cin>>dbl) )
       {
             cin.clear();
             cin.ignore( 1000, '\n' ) ; // *** added
             cout<<"input a number please";
        }
ok.
Thank you for the fix.
  int value=0;
    cout<<"enter your play ";
    
    while (!(cin>>value))
    {
    cin.clear();
    cin.ignore(80,'\n');
    cout<<"enter a number please ";
    }
    
    cout<<"good";


This is my input check.
why does it work perfectly on letters and not for doubles??
If I input a letter it behaves perfectly, if I try a double it crashes..
Any idea why?
Topic archived. No new replies allowed.