#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
using namespace std;
class Stats
{
private :
	int count, i, flag;
	float average,sum,value[20];
	char again, s[10];

public :
	Stats(void);
	~Stats(void);

	void input(void);
	int num_check(char *s);
	void sum1 (void);
	void avrge (void);
	void display (void);
};
void Stats :: input(void)
{
	do	// this loop asks the user to enter values and calculates the sum of these values
		{
			flag = 0;
			cout << "\nEnter value [" << count+1 << "] : ";
			cin >> s;	// this line takes the value in as a string
			flag = num_check(s);
			if (flag) continue;	// skip this loop iteration if an invalid character has been entered
			value[count] = (float) atof(s);	// convert string to number
			if (value[count] != 0)count++;
		}
		while ((count < 20) && (value[count] != 0));	// this line checks to see if the array is full or if a zero has been entered
}
int Stats :: num_check (char *s)
{
	int i, flag = 0, flage = 0, flagpoint =0;
	for (i = 0; i < strlen(s); i++)	// this loop will check every character entered
	{
		if (i==0 && s[i] == 'e')
		{
			cout << "\nplease enter numbers only\n";	// message displayed if an invalid character found in the number
			flag = 1;
			break;
		}
		if (s[i] == 'e' && flage == 1)
		{
			cout << "\nplease enter numbers only\n";	// message displayed if an invalid character found in the number
			flag = 1;
			break;
		}
		if (s[i] == 'e') flage = 1;
		if (s[i] == '.' && flagpoint == 1)
		{
			cout << "\nplease enter numbers only\n";	// message displayed if an invalid character found in the number
			flag = 1;
			break;
		}
		if (s[i] == '.' && flage == 0) flagpoint = 1;
		if (s[i] == '.' && flage == 1)
		{
			cout << "\nplease enter numbers only\n";	// message displayed if an invalid character found in the number
			flag = 1;
			break;
		}
		if (!isdigit(s[i]) && s[i] != '.' && s[i] != 'e')	//this line checks to see if an alphabetic character has been entered
		{
			cout << "\nplease enter numbers only\n";	// message displayed if an invalid character found in the number
			flag = 1;
			break;
		}
	}
	return flag;
}
void Stats :: sum1 (void)
{
	int i=0;
	do	
	{
		sum = sum + value[i];	// this line sums the values entered
		i++;	// this line keeps count of how many numbers have been entered
	}
	while (i < count);	// this line checks to see if the array is full or if a zero has been entered
}
void Stats ::avrge (void)
{
	if (count > 0) average = sum/count;	// this line calculates the average of the numbers entered
	else average = 0;
}
void Stats ::display (void)
{
	cout << "you entered the following " << count << " numbers \n";
	for (i=0; i <count; i++)	// this loop displays the numbers which were entered
		cout << "\nvalue [" << i+1 << "] = " << value[i];
	cout << "\n\nThe sum of these numbers is " << sum;
	cout << "\nThe average of these numbers is " << average;
}
Stats :: Stats (void)
{
	average = 0;	// these lines initialise the variables used to calculate the sum and average
	count = 0;
	sum = 0;
	for (i = 0; i < 20; i++)	// this loop places -1 in every element of the array
		value[i] = -1;
}
Stats :: ~Stats (void)
{
}
void main (void)
{
	char again;
	do
	{
		Stats numbers;
		cout << "This program will calculate the sum and average of a list of numbers \n";
		cout << "the maximum number of values that can be entered is 20 \n";
		cout << "the user can end the list by entering a value of zero \n";
		
		numbers.input();
		numbers.sum1();
		numbers.avrge();
		numbers.display();

		do	// this loop will check that the reply is either y, Y, n, or N
		{
			cout << "\nDo you want to enter another list of numbers ? ";
			cin >> again;
		}
		while ((again != 'y') && (again != 'Y') && (again != 'n') && (again != 'N'));
	}
	while ((again == 'y') || (again == 'Y'));	// this line checks to see if the user wants another go
	cout << "\n\n	End of program	\n\n";
}