#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>

int main()
{
int i=0;

char ch;
char ch2;
char yn;

long offset, last;

const int maxlength = 21;
const int maxchars = 80;

char cfile[maxlength]= "test.dat";
char efile[maxlength]= "test.enc";

ifstream incoming;
ofstream encodeout;

cout << "Please enter the name of the file you want to decode" << endl;
cin.getline(efile, maxlength);

incoming.open(efile);

i = 0;
 while(efile[i] != '.')
 {
 cfile[i] = efile[i];
 i++;
 }
i++;
cfile[i] = 't';
i++;
cfile[i] = 'x';
i++;
cfile[i] = 't';
encodeout.open(cfile);

cout << "The file currently contains:" << endl;

while((ch=incoming.peek()) != EOF)
{
incoming.get(ch);
cout << ch;
}
cout << endl;

cout << "Do you want to decode this file? 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
    ch2 = ch;
    cout << ch2;
    encodeout << ch2;
  }
   cout << "\nYour decoded file has been saved as: " << cfile;
   cin >> i;
}

else
        return 0;
}
