Hi guys:
I wrote this simple code to read a series of numbers from a list and then do some simple aremathic with it. I am not certain where the error could be, I read the code upside down and still can understand where the error lies. Any help would be greatly appreciated.
Gus
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int averageResult(int average, int imageCount)
{
ifstream in("objv.sky");
ofstream out("objv.skyc");
string image;
int npix;
double mean, stddev, min,forSum,sum,avgPct;
vector<double> max (imageCount);
int subtractionNumber[imageCount];
getline(in, image); //to skip header
for (int i=0;i<imageCount;i++)
{
if (in >> image >> npix >> mean >> stddev >> min >> max[i])
{
subtractionNumber[i] = average - max[i];
return(subtractionNumber[i]);
}
}
}
int main()
{
ifstream in("objv.sky");
ofstream out("objv.skyc");
string image;
int npix,imageCount;
double mean, stddev, min,forSum,sum,average,avgPct;
vector<double> max (imageCount);
vector<double> imageDiff (imageCount);
getline(in, image); //to skip the header
cout << "How many images in the list: ";
cin >> imageCount;
for(int i=0;i<imageCount;i++)
{
average=0;
if(in >> image >> npix >> mean>> stddev >> min >> max[i])
{
cout <<"["<<i+1<<"] "<< max[i] << "\n";
sum += max[i];
}
}
average = sum/imageCount;
cout << "The average is : " << average << "\n";
cout << averageResult(average,imageCount) << "\n";
return 0;
}
The error
% g++ -o test test.cpp
% ./test
How many images in the list: 5
Segmentation fault
%
The file is reading
# IMAGE NPIX MEAN STDDEV MIN MAX
0b.imh 1048576 1984. 490.1 1870. 65535.
3c.imh 1048576 1984. 471. 1875. 65446.
4g.imh 1048576 1984. 479.1 1872. 65335.
3s.imh 1048576 1984. 473.8 1869. 65535.
1z.imh 1048576 1984. 476.3 1869. 65522.