/*
Ynori7
Encrypto v1.0
*/
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

void encrypt(string received);
void decrypt(string received);
void getStuff();
void outputStuff(string out);

int main()
{
	getStuff();

	system("PAUSE");
	return 0;
}

//sends stuff to the data file
void outputStuff(string out)
{
	ofstream outfile;
	outfile.open("data.DAT",ios::out);
	if (outfile)
	{
		outfile<<out;
	}
	else
	{
		cout<<"An error occurred while opening the file.\n";
	}
 	outfile.close();
}

//retrieves stuff from the data file
void getStuff()
{
	ifstream infile;
	string s="", s1="";
	infile.open("data.dat",ios::in);

	if (infile) 
	{
		while (!infile.eof())
		{
			//infile.ignore(5, '\n');
			getline(infile, s1);
			s+=s1;
		}
		infile.close();
	}
	else  
	{
		cout << "An error occurred while opening the file.\n";
	}
	//decides whether to encrypt or decrypt
	if(s.at(0) == '1')
	{ encrypt(s); }
	else
	{ decrypt(s); }

}

//encrypts the stuff
void encrypt(string received)
{
	string tbe=received, tbe1, e = "";
	vector <string> s;

	if(tbe.at(tbe.length()-1) != ' ')
	{ tbe+=' '; }

	for(int x=0; x<tbe.length(); x++)
	{
		if( tbe.at(x) == ' ' )
		{
			s.push_back( tbe.substr(0, x) );
			tbe = tbe.substr(x+1, tbe.length()-1);
			x=-1;
		}
	}

	for(x=0; x<s.size(); x++)
	{
		for(int y=0; y<s[x].length(); y++)
		{
			e+=s[x].at(y)+y;
		}
		e+=' ';
	}
	cout<<e<<endl;
	outputStuff(e);
}

//decrypts the stuff
void decrypt(string received)
{
	string tbe=received, tbe1, e = "";
	vector <string> s;
	
	if(tbe.at(tbe.length()-1) != ' ')
	{ tbe+=' '; }

	for(int x=0; x<tbe.length(); x++)
	{
		if( tbe.at(x) == ' ' )
		{
			s.push_back( tbe.substr(0, x) );
			tbe = tbe.substr(x+1, tbe.length()-1);
			x=-1;
		}
	}

	for(x=0; x<s.size(); x++)
	{
		for(int y=0; y<s[x].length(); y++)
		{
			e+=s[x].at(y)-y;
		}
		e+=' ';
	}
	cout<<e<<endl;
	outputStuff(e);
}