myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> i)
{
if (i > N)
max = i;
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> i)
{
if (i < N)
min = i;
}
cout << endl << endl;
cout << "The sum of the data is: " << sum << endl;
cout << "The average value of the data is: " << mean << endl;
cout << "The biggest number is: " << max << endl;
cout << "The smallest number is: " << min << endl;
myfilein.close(); // close file
return 0;
}
And also you should initialize 'max' with a really low value and 'min' with a really high value so that will work fine with any input from the file
eg: if you initialize mix with 0 ( as you are doing now ) and the minimum value in the file is 1, you will get 0 as minimum value even if it isn't present in the file. To find the maximum or minimum value for a numeric type, use numeric_limits: http://www.cplusplus.com/reference/std/limits/numeric_limits/
Thanks that worked a treat :)
My only problem is with the average. I need it to count the amount of numbers in the file so i can use that to divide the sum of the data.
int number_of_numbers = 0;
while (myfilein >> i)
{
number_of_numbers++; // increase number_of_numbers by one
sum += i;
}
if ( number_of_numbers ) // don't divide by zero
average = sum / number_of_numbers; // sum and average should be doubles to get a good result
else
// you didn't read any number