更新时间: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.