Hi, I have a problem with a program that shows the contents of a file, but when it process the data to figure out the sum, max, min and average, that values are wrong.
Any ideas?
Thanks Liam
// CO1401 Reassessment 2008/9
//Liam Moncur BSc Computer Networking
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
double max(const double *data, int N);
double min(const double *data, int N);
double sum(const double *data, int N);
double mean(const double *data, int N);
int main ()
{
char N, filein[10];
ifstream myfilein;
double sum=0;
double mean=0;
double max=0;
double min=0;
const int NMAX = 10; // Maximum number of items to be processed
do
{
cout << "Enter the name of an existing text file: ";
cin.get (filein,10);
myfilein.open (filein);
if (!myfilein.is_open())
{
cout << "Unable to open file, sorry!" << endl;
cout << "Please enter the file name that you which to be processed : ";
cin.get (filein, 10);
}
}
while (!myfilein.is_open());
cout << "These are the contents of the file: " << endl;
while (myfilein.good()) // loop while extraction from file is possible
{
myfilein.get(N); // get character from file
if (myfilein.good())
cout << N;
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
sum += N;
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
mean = sum/5;
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
if (filein[10] > N)
max = filein[10];
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
if (filein[10] < N)
min = filein[10];
}
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;
}