Help getting maximum and minimum from .txt file list

Hello, first time posting here. Have some experience programming but relatively new. I have this program that takes a list of numbers from a .txt file and finds the mean, root-mean-square and then finally I need to find the difference between the largest and smallest values. The problem arises with the if else statement to find the largest and smallest values. The program returns no errors when compiled but does not run. The output screen is just blank with a blinking cursor. Can anyone help? Thanks in advance.

#include<iostream> // header which contains the functions needed for input and output
using namespace std;
#include<fstream>
#include<math.h>


double data, mean, Rq, smallest=0, largest=0, diff;
double sum=0, Rqsum=0;

int main()
{
ifstream infile("xxxx3padata.txt", ios::in);
infile>>data;
while(! infile.eof()) //loop to run data from text file
{
sum=sum+fabs(data); //computes sum to use for mean
Rqsum=Rqsum+pow(data,2); //computes numerator for RMS avs

//loop to find largest and smallest numbers
if(data<smallest)
{
smallest=data;
}
else if(data>largest)
{
largest=data;
}
}


mean=sum/56;
Rq=sqrt((Rqsum)/56);
diff=largest-smallest;
//output to user
cout<<"Arithmetic mean value: "<<mean<<endl;
cout<<"RMS average: "<<Rq<<endl;
cout<<"The difference between peaks is "<<diff<<endl;
}
you're reading the file only once through infile >> data;
also looping on eof() is not a good idea:
 
while(! infile.eof()) //loop to run data from text file  

http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong
http://stackoverflow.com/questions/5837639/eof-bad-practice
finally, using magic numbers like 56 makes me uncomfortable, let the program decide how many numbers are there in the file and the best way to do that is to read the file into a std::vector<double> and take it from there:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iterator>
#include <vector>
#include <fstream>

int main()
{
    std::ifstream inFile{"D:\\data.txt"};
    std::vector<double> v(std::istream_iterator<double>(inFile), {});
    //http://stackoverflow.com/questions/14516915/read-numeric-data-from-a-text-file-in-c
    //particularly see Jerry Coffin's answer in above link
   for (size_t i = 0; i < v.size(); ++i)std::cout << v[i] << "\n";
}
/*input file: 1.2 2.3 4.56 7.8 9.10
program output:
1.2
2.3
4.56
7.8
9.1
*/
Thanks, so much for the tips! I was able to get it to work!
Perhaps also worth mentioning the algorithm std::minmax_element to find the smallest and largest elements in a range: http://en.cppreference.com/w/cpp/algorithm/minmax_element
Topic archived. No new replies allowed.