Why the following c++ code keeps output “bad data, try again”?

后端 未结 6 2034
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 21:34
int main()
{
        int temp;
        while (cin>>temp, !cin.eof())
        {
                if (cin.bad())
                {
                        throw r         


        
6条回答
  •  温柔的废话
    2021-01-14 22:05

    Because your loop will end only when end-of-file is reached. But if you try to read a number where there is an alpha character, cin will fail but eof will never be reached. It will fail to read the number over and over again.

提交回复
热议问题