#include <iostream>
using namespace std;
bool menu();                 
void Initialization();
void Reset();
void Print();
void Quit();    

char firstChar;
int no_rows;
int no_columns;
bool initialized = false;


int main()
{
    bool exitflag =0;
    while (exitflag == 0) // The loop will stop when meun() returns true
       exitflag = menu();
    return 0;
}

bool menu() // Function menu()
{
    char choice;

    cout<<"=== MENU ==="<<endl;
    cout<<"[I] Initialization"<<endl;
    cout<<"[R] Reset"<<endl;
    cout<<"[P] Print"<<endl;
    cout<<"[Q] Exit\n"<<endl;
    cout<<"Enter your choice (I, R, P, or Q):";
    cin>>choice;
    switch (choice) {
       case 'I':
          Initialization(); 
		  break;
       case 'R':
          Reset(); 
		  break;
       case 'P':
          Print(); 
		  break;
       case 'Q':
          Quit();
          return true;
       default:
          cout<<"Unacceptable input, please enter again!"<<endl<<endl; 
		  break;
    }
    return false;
}

/* Ask for user inputs for firstChar, no_rows, and no_columns
   This function must be executed once and before executing
   Reset() and Print().
   If Reset() or Print() is called before calling this function,
   Return an error message “Please initialize the character and the 
   Numbers of rows and columns first!” and display the menu again.
*/
void Initialization()
{
	 if (initialized == true)
	 {
		 cout<<"You have already performed initialization. Please use 'R' to reset the parameters.\n";
		 return;
	 }
	 cout<<"Please enter the first character:\n";
     cin>>firstChar;
	 while (!(('a' <= firstChar && firstChar <= 'z') || ('A' <= firstChar && firstChar <= 'Z')))
	 {
		 cout<<"Please enter an alphabet in upper or lower case:\n";
         cin>>firstChar;
	 }
	 cout<<"Please enter the number of rows:\n";
     cin>>no_rows;
	 while (no_rows <= 0)
	 {
		 cout<<"enter a positive number:\n";
         cin>>no_rows;
	 }
	 cout<<"Please enter the number of columns:\n";
     cin>>no_columns;
 	 while (no_columns <= 0)
	 {
		 cout<<"enter a positive number:\n";
         cin>>no_columns;
	 }

	 initialized = true;
	 return;
}

/* Ask for user inputs to reset the values of firstChar, no_rows, 
   If Reset()is called before calling Initialization(), return an 
   error message “Please initialize the character and the 
   Numbers of rows and columns first!” and display the menu again.
*/
void Reset()
{
	 if (initialized == false)
	 {
		 cout<<"Please select 'I' first.\n";
		 return;
	 }

	 cout<<"Please reset the first character to:\n";
     cin>>firstChar;
	 while (!(('a' <= firstChar && firstChar <= 'z') || ('A' <= firstChar && firstChar <= 'Z')))
	 {
		 cout<<"Please enter an alphabet in upper or lower case:\n";
         cin>>firstChar;
	 }
	 cout<<"Please reset the number of rows to:\n";
     cin>>no_rows;
	 while (no_rows <= 0)
	 {
		 cout<<"enter a positive number:\n";
         cin>>no_rows;
	 }
	 cout<<"Please reset the number of columns to:\n";
     cin>>no_columns;
 	 while (no_columns <= 0)
	 {
		 cout<<"enter a positive number:\n";
         cin>>no_columns;
	 }

	 return;
}

/* Print out the array of characters.
   If Print()is called before calling Initialization(), return an 
   error message “Please initialize the character and the 
   Numbers of rows and columns first!” and display the menu again.
*/
void Print()
{
	if (initialized == false)
	{
     	cout<<"Please select 'I' first.\n";
	    return;
	}

    int posChar;
	char theChar;

	if (firstChar <= 'Z')
		posChar = firstChar - 65;
	else
		posChar = firstChar - 97;

	for (int i=0; i<no_rows; i++, posChar++)
	{	
		int pos1Char=posChar;

		for (int j=0; j<no_columns; j++, pos1Char++)
		{
			if (firstChar <= 'Z')
				theChar = 'A' + pos1Char%26;
			else
			    theChar = 'a' + pos1Char%26;
			cout << theChar;
		}
		cout << "\n";
	}
    return;
}

/* Quit the program and return to the command prompt.
*/
void Quit()
{
    cout<<"Thank you for using this program."<<endl<<endl;
    return;

}
