std::cin.clear() fails to restore input stream in a good state

前端 未结 3 739
心在旅途
心在旅途 2021-01-22 12:04

In order to test bool i/o, I tried to run this short program:

#include 

int main()
{
 while(true)
 {
  bool f;
  if (std::cin >> f)
   std         


        
3条回答
  •  一生所求
    2021-01-22 12:16

    Add this line after clearing cin:

    std::cin.ignore();
    

    This way, the stream ignores whatever is left on its buffer.

提交回复
热议问题