// Solution of Assignment 6.
#include <iostream>
using namespace std;
int main()
{
	int n, sum, product, i;
	char repeat, choice;

	repeat = 'Y';
	while (toupper(repeat) == 'Y')
	{
		// Clear the screen
		system("cls");

		// Ask for a positive integer > 1.
		cout << "Enter a positive integer greater than 1: ";
		cin >> n;
		/* If the user entered an integer less than 2, keep asking 
			until he/she enters an integer > 1.
		*/
		while (n < 2)
		{
			cout << "\a\n\t\t ======== Invalid input ========\n\n";
			cout << "Enter a positive integer greater than 1: ";
			cin >> n;
		}

		/* At this stage, we're sure the user entered an integer > 1.
			So, we can display the menu.
		*/
		cout << "\n";
		cout << "- Enter 1 to find the sum of the integers from 1"
			<< " to " << n << ".\n";
		cout << "- Enter 2 to find n! "
			 << "(the product of the integers from 1 to"
			 << n << ".\n";
		cout << "\t\t\tYour choice: ";
		cin >> choice;

		/* The choice should be 1 or 2 (as a character). 
			If the user entered a character different than that, 
			keep asking until he/she enters 1 or 2.
		*/
		while (choice != '1' && choice != '2')
		{
			cout << "\a\n\t\t ======== Invalid input ========\n\n";
			cout << "\t\t\tYour choice: ";
			cin >> choice;
		}
		
		/* At this stage, we're sure the user entered 1 or 2. 
			So, we can process the user's request.
		*/
		if (choice == '1') // Be careful, do not write (choice == 1)
		{
			sum = 0;
			for (i = 1; i <= n; i++)
				sum += i;
			cout << "\n===============================================\n";
			cout << "The sum of the integers from 1 to "
				<< n << " is " << sum << ".";
			cout << "\n===============================================\n";
		}
		else if (choice == '2')
		{
			product = 1;
			for (i = 1; i <= n; i++)
				product *= i;
			cout << "\n===============================================\n";
			cout << "The product of the integers from 1 to "
				<< n << " is " << product << ".";
			cout << "\n===============================================\n";
		}

		cout << "\n\tTo continue, press 'y' or 'Y': ";
		cin >> repeat;
	}

	cout << endl;
	return 0;
}
