且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++使用#iostream检查输入是否为float

更新时间:2023-11-28 13:48:46

> cin>> num 无法读取一个数字,然后流被置于失败状态(即,设置 failbit ),读过去导致它失败的字符。您从不对 clear()失败状态或 ignore()执行任何操作,因此您永远循环。

If cin >> num fails to read a number then the stream is put into the failed state (that is, the failbit is set), and it doesn't read past the character that caused it to fail. You never do anything to clear() the fail state or to ignore() the bad data, so you loop forever.