#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <string.h>

int i=0;
int k=0;
int chk=0;
char j='a';

char ch;
char ch2;
char yn;

long offset, last;

const int maxlength = 21;
const int maxchars = 80;

char cfile[maxlength]= "test.txt";
char efile[maxlength]= "test.enc";
char message[maxchars]= "hello there";

ifstream incoming;
ofstream encodeout;

void encode(void);
void decode(void);
void mencode(void);

int main()
{
while (i == 0)
{
cout << "\n                 Welcome to jambeCodec Version 1.1 " << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "                              Menu                                " << endl;
cout << "Press: \nE for encode, D for decode, M for manual encode, \nH for help, A for about, or X for exit" << endl;
cin >> yn;

if(yn == 'E' || yn == 'e')
{
i = 1;
encode();
}
else if(yn == 'D' || yn == 'd')
{
i = 1;
decode();
}
else if(yn == 'X' || yn == 'x')
{
i = 1;
}
else if(yn == 'h'||yn == 'H')
{
cout << "\n\n------------------------------------------------------------------" << endl;
cout << "                     +jambeCodec Help+ " << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "\n\nto encode a message you need to make a plain text file \n  (you can make one in a program like notepad)" << endl;
cout << "You can save you text file with any name you like \nbut make sure you save it to the same directory as this program." << endl;
cout << "If you save it some where other than this programs directory \nthen you will have to use the full path ie. C:\\codes\\test.txt" << endl;
cout << "New In this version you can Manualy encode a file. \nThis means that you type your message directly into this program."<< endl;
cout << "Currently jambeCodec will only encode or decode one file at a time \nIe. if you want to encode test.txt and then decode it \nyou will have to restart between each one." << endl;
cout << "\n\nPress any key and enter to return to the Menu:" <<endl;
cin >> j;
}
else if(yn == 'i'||yn == 'I'||yn == 'a'||yn == 'A')
{
cout << "\n\n------------------------------------------------------------------" << endl;
cout << "                     +About jambeCodec+" << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "\n\nTo find out how to use jambeCodec see help" << endl;
cout << "\nWhat is jambeCodec?" << endl;
cout << "jambeCodec is a program that turns plain text in to a secret code." << endl;
cout << "\nHow does it Work?"<< endl;
cout << "jambeCodec has a secret code that governs how it encodes. \nTry to figure it out it is quite simple" << endl;
cout << " \n             more (press any key & enter)" << endl;
cin >> ch;
cout << "\n\nWhat is New in version 1.1?" << endl;
cout << "Version 1.1 includes the following advancements:" << endl;
cout << "1. Manual Encodeing" << endl;
cout << "2. This about file" << endl;
cout << "3. Smart Warnings about empty files" << endl;
cout << "4. Updated help" << endl;
cout << "\nKnown Bugs:" << endl;
cout << "1. Only able to encode or decode once without restarting jambeCodec" << endl;
cout << "2. Multi Lined files after decoding contain \ntwo blank lines for each newline in the original file" << endl;
cout << " If you have any Ideas about how to fix these problem's or you find more" << endl;
cout << "         PLEASE email me at jambe@zdnetmail.com" << endl;
cout << "\n If there is anything you would like to see added to jambeCodec or "<< endl;
cout << "   You have invented a secret code and would like it made in to a progam "<< endl;
cout << "         PLEASE email me at jambe@zdnetmail.com" << endl;
cout << "\n For more information and the latest updates check out www.jambe.cjb.net" << endl;
cout << "    Don't forget jambe corp's partner online www.bbrunskill.cjb.net" << endl;
cout << "\n         - JAMES " << endl;
cout << "\nPress any key and enter to return to the Menu:" <<endl;
cin >> j;
}
else if(yn == 'm'||yn == 'M')
{
mencode();
}
else
cout << "Error! Try again:" << endl;
}

        return 0;
}

void encode()
{

cout << "Please enter the name of the file you want to encode:" << endl;

cin.ignore(); //to clean up after the earlier cin command
cin.getline(cfile, maxlength);

incoming.open(cfile);

i = 0;
 while(cfile[i] != '.')
 {
 efile[i] = cfile[i];
 //cout << "cfile[" << i << "] is " << cfile[i] << endl;
 //cout << "efile[" << i << "] is " << efile[i] << endl;
 i++;
 }
efile[i] = '.';
i++;
efile[i] = 'e';
i++;
efile[i] = 'n';
i++;
efile[i] = 'c';
i++;
efile[i] = '\0';

encodeout.open(efile);

cout << "The file currently contains:" << endl;

while((ch=incoming.peek()) != EOF)
{
incoming.get(ch);
cout << ch;
if (ch !=EOF)
{
chk++;
}
}
cout << endl;

cout << "Do you want to encode this file? Y/N" << endl;

cin >> yn;

if (yn == 'Y' || yn == 'y')
{
if (chk == 0)
{
cout << "This File doesn't appear to contain anything! Are SURE you want to continue Y/n" << endl;
cin >> yn;
}
if (yn == 'Y' || yn == 'y')
{
  incoming.seekg(0L,ios::end);   // move to the end of the file
  last = incoming.tellg();
cout << "   Encoded Output:" << endl;
   for(offset = 1L; offset <= last; offset++)
  {
    incoming.seekg(-offset, ios::end);
    ch = incoming.get();
    if (ch == 'a')
    {
    ch2 = '1';
    //cout << "Small a detected" << endl;
    }
    else if (ch == 'e')
    {
    ch2 = '2';
    //cout << "Small e detected" << endl;
    }
    else if (ch == 'i')
    {
    ch2 = '3';
    //cout << "Small i detected" << endl;
    }
    else if (ch == 'o')
    {
    ch2 = '4';
    //cout << "Small o detected" << endl;
    }
    else if (ch == 'u')
    {
    ch2 = '5';
    //cout << "Small u detected" << endl;
    }
    else if (ch == 'A')
    {
    ch2 = '6';
    //cout << "Capital A detected" << endl;
    }
    else if (ch == 'E')
    {
    ch2 = '7';
    //cout << "Capital E detected" << endl;
    }
    else if (ch == 'I')
    {
    ch2 = '8';
    //cout << "Capital I detected" << endl;
    }
    else if (ch == 'O')
    {
    ch2 = '9';
    //cout << "Capital O detected" << endl;
    }
    else if (ch == 'U')
    {
    ch2 = '0';
    //cout << "Capital U detected" << endl;
    }
        else if (ch == '1')
    {
    ch2 = 'a';
    //cout << "Number 1 detected" << endl;
    }
    else if (ch == '2')
    {
    ch2 = 'e';
    //cout << "Number 2 detected" << endl;
    }
    else if (ch == '3')
    {
    ch2 = 'i';
    //cout << "Number 3 detected" << endl;
    }
    else if (ch == '4')
    {
    ch2 = 'o';
    //cout << "Number 4 detected" << endl;
    }
    else if (ch == '5')
    {
    ch2 = 'u';
    //cout << "Number 5 detected" << endl;
    }
    else if (ch == '6')
    {
    ch2 = 'A';
    //cout << "Number 6 detected" << endl;
    }
    else if (ch == '7')
    {
    ch2 = 'E';
    //cout << "number 7 detected" << endl;
    }
    else if (ch == '8')
    {
    ch2 = 'I';
    //cout << "Number 8 detected" << endl;
    }
    else if (ch == '9')
    {
    ch2 = 'O';
    //cout << "Number 9 detected" << endl;
    }
    else if (ch == '0')
    {
    ch2 = 'U';
    //cout << "Number 0 detected" << endl;
    }
    else
    ch2 = ch;
    cout << ch2;
    encodeout << ch2;
  }
 i = 0;

   cout << "\nYour encoded file has been saved as: " << efile;
   cout << "\n\nPress any Key and then Enter to return to main menu" << endl;
   cin >> j;
}
else
;
}
else
        i = 0;
        return;
}

void decode()
{
cout << "Please enter the name of the file you want to decode" << endl;

cin.ignore(); //to clean up after the earlier cin command
cin.getline(efile, maxlength);

incoming.open(efile);


i = 0;
 while(efile[i] != '.')
 {
 cfile[i] = efile[i];
 i++;
 }
cfile[i] = '.';
i++;
cfile[i] = 't';
i++;
cfile[i] = 'x';
i++;
cfile[i] = 't';
i++;
cfile[i] = '\0';
encodeout.open(cfile);

cout << "The file currently contains:" << endl;

while((ch=incoming.peek()) != EOF)
{
incoming.get(ch);
cout << ch;
if (ch !=EOF)
{
chk++;
}
}
cout << endl;

cout << "Do you want to decode this file? Y/N" << endl;
cin >> yn;

if (yn == 'Y' || yn == 'y')
{
if (chk==0)
{
cout << "This File doesn't appear to contain anything! Are SURE you want to continue Y/n" << endl;
cin >> yn;
}
if (yn == 'Y' || yn == 'y')
{
  incoming.seekg(0L,ios::end);   // move to the end of the file
  last = incoming.tellg();
cout << "  Decoded output :" << endl;
   for(offset = 1L; offset <= last; offset++)
  {
    incoming.seekg(-offset, ios::end);
    ch = incoming.get();
    if (ch == '1')
    {
    ch2 = 'a';
    //cout << "Small a detected" << endl;
    }
    else if (ch == '2')
    {
    ch2 = 'e';
    //cout << "Small e detected" << endl;
    }
    else if (ch == '3')
    {
    ch2 = 'i';
    //cout << "Small i detected" << endl;
    }
    else if (ch == '4')
    {
    ch2 = 'o';
    //cout << "Small o detected" << endl;
    }
    else if (ch == '5')
    {
    ch2 = 'u';
    //cout << "Small u detected" << endl;
    }
    else if (ch == '6')
    {
    ch2 = 'A';
    //cout << "Capital A detected" << endl;
    }
    else if (ch == '7')
    {
    ch2 = 'E';
    //cout << "Capital E detected" << endl;
    }
    else if (ch == '8')
    {
    ch2 = 'I';
    //cout << "Capital I detected" << endl;
    }
    else if (ch == '9')
    {
    ch2 = 'O';
    //cout << "Capital O detected" << endl;
    }
    else if (ch == '0')
    {
    ch2 = 'U';
    //cout << "Capital U detected" << endl;
    }
    else if (ch == 'a')
    {
    ch2 = '1';
    //cout << "number 1 detected" << endl;
    }
    else if (ch == 'e')
    {
    ch2 = '2';
    //cout << "number 2 detected" << endl;
    }
    else if (ch == 'i')
    {
    ch2 = '3';
    //cout << "number 3 detected" << endl;
    }
    else if (ch == 'o')
    {
    ch2 = '4';
    //cout << "number 4 detected" << endl;
    }
    else if (ch == 'u')
    {
    ch2 = '5';
    //cout << "number 5 detected" << endl;
    }
        else if (ch == 'A')
    {
    ch2 = '6';
    //cout << "Number 6 detected" << endl;
    }
    else if (ch == 'E')
    {
    ch2 = '7';
    //cout << "Number 7 detected" << endl;
    }
    else if (ch == 'I')
    {
    ch2 = '8';
    //cout << "Number 8 detected" << endl;
    }
    else if (ch == 'O')
    {
    ch2 = '9';
    //cout << "Number 9 detected" << endl;
    }
    else if (ch == 'U')
    {
    ch2 = '0';
    //cout << "Number 0 detected" << endl;
    }
    else
    ch2 = ch;
    cout << ch2;
    encodeout << ch2;
  }
   cout << "\nYour decoded file has been saved as: " << cfile;
   cout << "\n\nPress any Key and then Enter to exit" << endl;
   cin >> j;
}
}
else
        i = 0;
	encodeout.close();
incomming.close();
        return;
}

void mencode()
{

cout << "Please enter a name for the message you want to encode (no spaces):" << endl;

cin.ignore(); //to clean up after the earlier cin command
cin.getline(cfile, maxlength);

cout << "Please enter your message" << endl;
message[0]= ' ';
i=1;
while(i < maxchars && (ch = cin.get()) != '\n')
{
message[i]= ch;
i++;
}
message[i]='\0';
cout << "the message you just typed was:\n" << message << endl;

i = 0;
 while(cfile[i]!= ' '&& cfile[i]!= '.'&& cfile[i]!= '\0')
 {
 efile[i] = cfile[i];
 //cout << "cfile[" << i << "] is " << cfile[i] << endl;
 //cout << "efile[" << i << "] is " << efile[i] << endl;
 i++;
 }
efile[i] = '.';
i++;
efile[i] = 'e';
i++;
efile[i] = 'n';
i++;
efile[i] = 'c';
i++;
efile[i] = '\0';

encodeout.open(efile);

cout << "Do you want to encode this Message? Y/N" << endl;
cin >> yn;

if (yn == 'Y' || yn == 'y')
{
  cout << "   Encoded Output:" << endl;
  last = strlen(message);
  //cout << "strlen is " << last << endl;

   for(i = last; i != 0; i--)
  {
    ch = message[i];
    if (ch == 'a')
    {
    ch2 = '1';
    //cout << "Small a detected" << endl;
    }
    else if (ch == 'e')
    {
    ch2 = '2';
    //cout << "Small e detected" << endl;
    }
    else if (ch == 'i')
    {
    ch2 = '3';
    //cout << "Small i detected" << endl;
    }
    else if (ch == 'o')
    {
    ch2 = '4';
    //cout << "Small o detected" << endl;
    }
    else if (ch == 'u')
    {
    ch2 = '5';
    //cout << "Small u detected" << endl;
    }
    else if (ch == 'A')
    {
    ch2 = '6';
    //cout << "Capital A detected" << endl;
    }
    else if (ch == 'E')
    {
    ch2 = '7';
    //cout << "Capital E detected" << endl;
    }
    else if (ch == 'I')
    {
    ch2 = '8';
    //cout << "Capital I detected" << endl;
    }
    else if (ch == 'O')
    {
    ch2 = '9';
    //cout << "Capital O detected" << endl;
    }
    else if (ch == 'U')
    {
    ch2 = '0';
    //cout << "Capital U detected" << endl;
    }
    else if (ch == '1')
    {
    ch2 = 'a';
    //cout << "Number 1 detected" << endl;
    }
    else if (ch == '2')
    {
    ch2 = 'e';
    //cout << "Number 2 detected" << endl;
    }
    else if (ch == '3')
    {
    ch2 = 'i';
    //cout << "Number 3 detected" << endl;
    }
    else if (ch == '4')
    {
    ch2 = 'o';
    //cout << "Number 4 detected" << endl;
    }
    else if (ch == '5')
    {
    ch2 = 'u';
    //cout << "Number 5 detected" << endl;
    }
    else if (ch == '6')
    {
    ch2 = 'A';
    //cout << "Number 6 detected" << endl;
    }
    else if (ch == '7')
    {
    ch2 = 'E';
    //cout << "number 7 detected" << endl;
    }
    else if (ch == '8')
    {
    ch2 = 'I';
    //cout << "Number 8 detected" << endl;
    }
    else if (ch == '9')
    {
    ch2 = 'O';
    //cout << "Number 9 detected" << endl;
    }
    else if (ch == '0')
    {
    ch2 = 'U';
    //cout << "Number 0 detected" << endl;
    }
    else
    {
    ch2 = ch;
    }
    
    cout << ch2;
    encodeout << ch2;
  }
 i = 0;

   cout << "\nYour encoded file has been saved as: " << efile;
   cout << "\n\nPress any Key and then Enter to return to the menu" << endl;
   cin >> j;
}

else
        i = 0;
        return;
}
