0

What does this cin.clear() and cin.ignore() means?

this is the part of the code :

while(1) { 
      cout<<"Enter the Product ID :";
      cin>>product[i].id;
      cout<<endl;
      
      if(cin.fail())
        {
                      cout << "\t\t\t\t\WRONG ID DATA!" << endl;
                      cin.clear(); //what does it means??
                      cin.ignore(1000, '\n'); //why 1000??
                      continue;
        }
        break;
}

thank you for helping....

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jonsca
0

Clear clears the failbit (a flag (bit) that was set to true when your used typed in a letter for your id instead of a number). cin.fail() is what checks the bits to see if the failbit has been set and returns true if it was. At that time that it fails, the stream is no longer valid to take more input. Clear allows you to reset this failed state and once again take input into your stream.
http://www.cplusplus.com/reference/iostream/ios/clear/

Ignore in that case discards up to 1000 characters or until it reaches a newline (whichever comes first).
http://www.cplusplus.com/reference/iostream/istream/ignore/

Edited by jonsca: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.