I am making a encoder/decoder program. I have made most of it but I am stuck on the last bit. I would like when i decode the text it will oopen encode.txt, read it and change it back to normal (decode) the show it on the screen. If that cant be done could you do it so that it reads it, changes back to normal (decodes it) then saves it in the same encode.txt?
Heres the code:
/*
Name: Encryptor/Decryptor
Copyright: Just GFx & PSPhs
Author: Mako-Infused
Date: 26/10/08 16:44
Description:
*/
#include <iostream>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
const string cryptSymbols[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","A","B","C","D","E","F","G","H","I","J","K","L","-"};
const string normalSymbols[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0",".",","," "};
class crypt{
string inCode;
public:
crypt(string input);
string decode();
string encode();
};
crypt::crypt(string input){
inCode=input;
}
string crypt::decode(){
string output="";
string cryptLetter="";
char character;
char right;
int pointer=0;
while(pointer<inCode.length()){
character=inCode[pointer];
if(pointer<inCode.length()-1)
right=inCode[pointer+1];
if(!(character==' '))
cryptLetter+=character;
else{
for(int ctr=0;ctr<39;ctr++){
if(cryptLetter==cryptSymbols[ctr])
output+=normalSymbols[ctr];
}
cryptLetter="";
}
if((character==' ')&&(right==' ')){
output+=" ";
}
pointer++;
}
return output;
}
string crypt::encode(){
string output="";
string character="";
for(int ctr=0;ctr<inCode.length();ctr++){
character=inCode[ctr];
if(character==" ")
output+=" ";
for(int ctr2=0;ctr2<38;ctr2++){
if(character==normalSymbols[ctr2]){
output+=cryptSymbols[ctr2];
output+=" ";
}
}
}
return output;
}
int main(){
string input;
string output;
char choice;
char yn;
char number;
yn='y';
while((yn!='n')&&(yn!='N')){
cout<<"Welcome to Mako-Infused Encoder/Decoder\n";
cout<<"Please choose a option:\n";
cout<<"1 Encode Text\n";
cout<<"2 Decode Text\n";
cin>>choice;
cin.clear();
cin.ignore();
if(choice=='1'){
ofstream file; //declares file
file.open("encode.txt"); //opens file
cout<<"Type the Text you want to be encoded\n";
getline(cin,input);
crypt cryptCode(input+" ");
output=cryptCode.decode();
cout<<output<<'\n';
file<<cryptCode.encode()<<endl;
file.close();//closes
cout<<"(Your code has been saved at encode.txt)\n";
cin.clear();
cin.ignore();
}
else if (choice=='2'){
ifstream infile("encode.txt");
crypt cryptCode(input+" ");
output=cryptCode.decode();
getline(cin,input);
cout<<output<<'\n';
cout<<"-Finished...";
cin.clear();
cin.ignore();
}
return EXIT_SUCCESS;
}}