#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 encode" << endl;
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++;
 }
i++;
efile[i] = 'e';
i++;
efile[i] = 'n';
i++;
efile[i] = 'c';
encodeout.open(efile);

cout << "The file currently contains:" << endl;

while((ch=incoming.peek()) != EOF)
{
incoming.get(ch);
cout << ch;
}
cout << endl;

cout << "Do you want to encode 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();

   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
    ch2 = ch;
    cout << ch2;
    encodeout << ch2;
  }
 i = 0;
 while(cfile[i] != '.')
    {
    efile[i] = cfile[i];
    //cout << "cfile[" << i << "] is " << cfile[i] << endl;
    //cout << "efile[" << i << "] is " << efile[i] << endl;
    i++;
    }
    i++;
    efile[i] = 'e';
    i++;
    efile[i] = 'n';
    i++;
    efile[i] = 'c';
    encodeout.open(efile);

   cout << "\nYour encoded file has been saved as: " << efile;
   cin >> i;
}

else
        return 0;
}
