#include <iostream>

void menu (char );
int encrypt ( int );
int decrypt ( int );


int main ()
{
   char a; // identified a character to execute the menu func.
   std::cout << "**************************************\n" 
             << "* Integer Encryption/Decryption Tool *\n"
             << "**************************************\n" << std::endl;
   std::cout << "(E/e) Encryption" << std::endl;
   std::cout << "(D/d) Decryption" << std::endl;
   std::cout << "(Q/q) Quit" << std::endl;
   menu (a); // menu func starts here.
   return 0;
}


void menu ( char x )
{
   int a;
   char code; // code is the character that user will input
   std::cout << "Enter operation code: ";
   std::cin >> code;
   if ( code == 'e' || code == 'E')  encrypt(a);
   else if ( code == 'd' || code == 'D' ) decrypt(code);
   else if ( code == 'q' || code == 'Q' )  std::cout << "Bye!" << std::endl;
   else std::cout <<  "You entered an invalid operation code!" << std::endl; 
}



int encrypt (int a) 
{
   std::cout << "**********************\n" 
             << "* Encryption Process *\n" 
             << "**********************\n\n" 
             << "Enter the 4-digit integer to be encrypted : ";

   int digit,b;
   char cont;
   std::cin >> digit;

   int bas1,bas2,bas3,bas4;
   if ( 999 < digit  && digit < 10000 ) {
      bas1=digit/1000;
      bas2=digit/100 - 10*bas1;
      bas4= digit%10;
      bas3= (digit%100 -bas4)/10;
      std::cout << "Encrypted integer is " 
           << (bas1+7)%10
           << (bas2+7)%10 
           << (bas3+7)%10 
           << (bas4+7)%10 
           << "." << std::endl;
      return 0;      
   }
   else {
      std::cout << "You entered an invalid integer!"  << std::endl; 
      return 1;
   }
}



int decrypt ( int a) 
{
   std::cout << "**********************\n" 
             << "* Decryption Process *\n" 
             << "**********************\n\n" 
             << "Enter the 4-digit integer to be decrypted : ";

   int digit,c;
   std::cin >> digit;

   int bas1,bas2,bas3,bas4;
   if ( 999 < digit  && digit < 10000 ) {
      bas1=digit/1000;
      bas2=digit/100 - 10*bas1;
      bas4= digit%10;
      bas3= (digit%100 -bas4)/10;
      std::cout << "Decrypted integer is " << (bas1+3)%10 
           << (bas2+3)%10 
           << (bas3+3)%10 
           << (bas4+3)%10 
           << std::endl;
      return 0;
   }
   else {
      std::cout << "You entered an invalid integer!" << std::endl;
      return 1;
   }
}
