std::cin infinite loop from invalid input [duplicate]

寵の児 提交于 2019-12-02 01:15:09

By default, std::basic_istream::ignore() ignores only 1 character:

basic_istream& ignore( std::streamsize count = 1, int_type delim = Traits::eof() );

(From http://en.cppreference.com/w/cpp/io/basic_istream/ignore)

More idiomatic use for your case seems to be the one from the example there:

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Also, note that you want to call clear() before the ignore() call, so the input extraction of ignore() will succeed instead of returning immediately.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!