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

char firstChar;
int no_rows;
int no_columns;


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()
{
	 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;
	 }

	 if ('a' <= firstChar && firstChar <= 'z')
	     if (firstChar+no_rows+no_columns-2 > 122)
			 cout<<"Please decrease the number of rows/columns!\n";
	 if ('A' <= firstChar && firstChar <= 'Z')
	     if (firstChar+no_rows+no_columns-2 > 90)
			 cout<<"Please decrease the number of rows/columns!\n";

	 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()
{
	 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;
	 }

	 if ('a' <= firstChar && firstChar <= 'z')
	     if (firstChar+no_rows+no_columns-2 > 122)
			 cout<<"Please decrease the number of rows/columns!\n";
	 if ('A' <= firstChar && firstChar <= 'Z')
	     if (firstChar+no_rows+no_columns-2 > 90)
			 cout<<"Please decrease the number of rows/columns!\n";

	 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()
{
	for (int i=0; i<no_rows; i++, firstChar++)
	{	
		char aChar=firstChar;
		for (int j=0; j<no_columns; j++, aChar++)
			cout << aChar;
		cout << "\n";
	}
    return;
}

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

}
