#include <iostream>
#include <stdexcept>
#include <limits>
using namespace std;

int main(){
	cout << "Mean of n Numbers \n";
	cout << "To exit, n < 1 \n\n";
	while (true) {
		try{
			int n;
			cout << "n = ";
			cin >> n;
			if (cin.fail()){
				throw invalid_argument("Error! n has to be an integer!");
			}
			cin.clear(); 
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
			if (n<1) {
				cout << "The program is terminated. Bye!";
				break;
			}
			double s = 0;
			for (int i=0; i<n; i++) {
				double x;
				cout << "Number" << i+1 << " = ";
				cin >> x;
				if (cin.fail()){
					throw invalid_argument("Error! You have to enter numbers!");
				}
				s += x;
			}
			double m = s / n;
			cout << "Mean = " << m << endl;
			cout << endl;
		} catch (const invalid_argument& e){
			cin.clear(); 
            cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
			cout << e.what() << endl << endl;
		}
	}
	return 0;
}
