小编use*_*373的帖子

在我的程序中使用eof然后不断循环为什么?

可能重复:
为什么循环条件中的iostream :: eof被认为是错误的?

这是我编译的程序,除了使用eof的while循环之外的所有内容都变得无限,文件scores.dat包含20个随机数的列表.为什么eof不起作用并使其循环不断???

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main ()
{

  int x, sum = 0, count = 0;
  double answer;
  ifstream  y;

  y.open("scores.dat");
  while (!y.eof())
   {
     y >> x;
     sum = sum + x;
     count ++;
     cout << x << endl;
   }

  answer = sqrt (((pow(x, 2.0)) - ((1.0/count) * (pow(x, 2.0)))) / (count - 1.0));
  cout << answer;

}
Run Code Online (Sandbox Code Playgroud)

c++ infinite-loop while-loop eof

2
推荐指数
1
解决办法
246
查看次数

标签 统计

c++ ×1

eof ×1

infinite-loop ×1

while-loop ×1