gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 00:41:32 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Why is iostream::eof inside a loop condition considered wrong?

这是我编译的程序,除了带有 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;

}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com