double sum = 0;
double max = 0;
double min = 0;
do
{
myFile >> x;
sum += x;
if (!myFile.eof())
{
cout << x << endl;
count ++;
if(count%20==0)
{
double average = sum/count;
cout << "The Average is: " << average << endl;
cout << "The min is: " << min << endl;
cout << "The max is: " << max << endl;
system("pause");
}
}
} while (!myFile.eof());
This is my code and because the min is set to 0, no matter the number (unless it is negative) the min will be 0 even if it is 4 in the set of numbers. How do I get the min to be the lowest number in the set of numbers from a file?
Try moving the declarations of min and max under sum += x
and write
1 2
staticdouble min = x;
staticdouble max = x;
there instead. Not very elegant, but without completely changing the way you are doing this - which I would actually recommend - you won't get around this.