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;
}

First, your code with decent indentation. FYI, turn off TABS in your editor, and set it to use spaces all the time.

// 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;
}

@line50 is OK
@line61 has NO dependence on the file, you just do the same calculation over and over. Also, you're assuming 5 values were read.
@68 and 76 should be comparing with N, not some character off the end of your filename char array.

@Salem
Why would you use spaces instead of tabs? I never understood that, I always set it to tabs - then one keystroke/character replaces several, no?

@Salem
Why would you use spaces instead of tabs? I never understood that, I always set it to tabs - then one keystroke/character replaces several, no?

Yes, but if set to spaces, pressing the TAB key can add 4 spaces which makes code much easier to read. And when posted here, where tabs are always screwy, helps us read your code.

Tabs are OK, if (AND ONLY IF) you use them for EVERY indent.

As soon as you mix them in the code, which is very easy to do, the result when posted on a forum is a mess.

I see this has turned into yet another meta-discussion about something other than the original problem :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.