Saturday 14 September 2019

c++ - Using eof in my program and then continuously loops Why?








Here is my program it compiles and everything but the while loop with eof becomes infinite
the file scores.dat contains a list of 20 random numbers. Why does the eof not work and makes it loop continuously???



#include 
#include

#include
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;

}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...